Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing date format javascript

I'm pulling some data from two different APIs and I want to the objects later on.

However, I'm getting two different date formats: this format "1427457730" and this format "2015-04-10T09:12:22Z". How can I change the format of one of these so I have the same format to work with?

$.each(object, function(index) {
  date = object[index].updated_at;
}
like image 532
Stef Kors Avatar asked Mar 09 '26 09:03

Stef Kors


1 Answers

Here's one option:

var timestamp  = 1427457730;
var date       = new Date(timestamp * 1000); // wants milliseconds, not seconds
var dateString = date.toISOString().replace(/\.\d+Z/, 'Z'); // remove the ms

dateString will now be 2015-03-27T12:02:10Z.

like image 127
robertklep Avatar answered Mar 11 '26 07:03

robertklep