Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Convert a UTC Date() object to the local timezone

I have a Date() object that contains a UTC date, which I need converted to the users local timezone. Does anybody know how I could do this? :-)

like image 753
tarnfeld Avatar asked Sep 18 '10 10:09

tarnfeld


People also ask

How do I convert UTC to local date?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

What is UTC in JavaScript?

UTC() method in JavaScript is used to return the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time. The UTC() method differs from the Date constructor in two ways: Date. UTC() uses universal time instead of the local time.

Is new date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

Does JSON Stringify convert date to UTC?

stringify converts DateTimeOffset to UTC format #1710.


2 Answers

I usually create a new Date object and use the Date.setUTC* functions to copy the date information.

like image 160
Radomir Dopieralski Avatar answered Oct 23 '22 04:10

Radomir Dopieralski


I'm pretty sure it is done for you automatically.

>>> d = new Date('Fri, 10 Jun 2011 19:49:23 UTC');
Sat Jun 11 2011 07:49:23 GMT+1200 (New Zealand Standard Time)
>>> d.getHours();
7
like image 25
Keyo Avatar answered Oct 23 '22 03:10

Keyo