Kal
Posts: 0
Joined: Thu May 22, 2014 11:03 pm

Converting _createdAt timestamp to local time

Hi, I am trying to convert the _createdAt timestamp (which is in UTC) to display local time with the following code:

d = new Date(value+' UTC');
return(d.toString());

However, I get "Invalid Date". How do I accomplish this?

Extra question: I want to display it like mm/dd/yy hh:mm format. How is this done?

Thanks!

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Converting _createdAt timestamp to local time

Hi Kal,

Please use following code instead of yours:

pre

var stringDate = value;

var arrDate = stringDate.split(/[- :]/);
var date = new Date(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3], arrDate[4]);

/pre

Also you can format date string like you need using methods from "date" object you have.

Like: getDate(), getFullMonth see details about it here: http://www.w3schools.com/jsref/jsref_...

Regards

Kal
Posts: 0
Joined: Thu May 22, 2014 11:03 pm

Converting _createdAt timestamp to local time

Thanks, that worked.

Kal
Posts: 0
Joined: Thu May 22, 2014 11:03 pm

Converting _createdAt timestamp to local time

Oops, spoke too soon. This method does NOT convert the date to the local time zone; instead, it just uses the UTC time as if it were in the local time zone. For example, the stringDate is : 2014-07-15 04:45:49.444

When converted to Date using the method above, the printed out date is: Tue Jul 15 2014 04:45:00 GMT-0700 (PDT). In other words, it just assumes the time is already in PDT, when it is not.

I tried this, and this seems to work: var date = new Date(Date.UTC(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3]-1, arrDate[4]-1));

Kal
Posts: 0
Joined: Thu May 22, 2014 11:03 pm

Converting _createdAt timestamp to local time

Katya, I did try that before I posted here, but could not make it work. The solution I have given above does work (which is a modification of the solution given by Yurii), so this question can be marked as closed; I was just documenting it for anyone that is searching the archives.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Converting _createdAt timestamp to local time

Hi Kal,

Thanks for your update.

Regards.

Anim
Posts: 0
Joined: Fri Jul 03, 2015 10:45 am

Converting _createdAt timestamp to local time

Hi Kal,
Can you please post your solution here.

Animesh

Return to “Issues”