Page 1 of 1

Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 4:58 am
by Kal

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!


Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 5:12 am
by Yurii Orishchuk

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


Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 3:53 pm
by Kal

Thanks, that worked.


Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 4:20 pm
by Kal

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));


Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 9:23 pm
by Kateryna Grynko

Hi Kal,

You could try this solution:
http://stackoverflow.com/questions/19...


Converting _createdAt timestamp to local time

Posted: Tue Jul 15, 2014 9:28 pm
by Kal

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.


Converting _createdAt timestamp to local time

Posted: Wed Jul 16, 2014 1:45 am
by Yurii Orishchuk

Hi Kal,

Thanks for your update.

Regards.


Converting _createdAt timestamp to local time

Posted: Thu Aug 27, 2015 8:08 am
by Anim

Hi Kal,
Can you please post your solution here.

Animesh