Javascript to convert UTC to local time

Ok, say that JSON will mark up the UTC string as shown below:

2012-11-29 17:00:34 UTC 

Now, if I want to convert this UTC date to my local time, how can I do this?

thanks for the answer

=====

Then, how do I format it for something else, like "yyyy-MM-dd HH: mm: ss z ??

This date.toString('yyyy-MM-dd HH:mm:ss z'); never works: /

+33
javascript datetime
Nov 29 '12 at 9:03
source share
6 answers

Try:

 var date = new Date('2012-11-29 17:00:34 UTC'); date.toString(); 
+43
Nov 29 '12 at 9:06
source share
 var offset = new Date().getTimezoneOffset(); 

offset will be the interval in minutes from local time to UTC. To get the local time with a UTC date, then you subtract the minutes from your date.

 utc_date.setMinutes(utc_date.getMinutes() - offset); 
+29
Nov 29 '12 at 9:18
source share

This should work

 var date = new Date('2012-11-29 17:00:34 UTC'); date.toString() 
+1
Nov 29 '12 at 9:06
source share

To format the date, try the following function:

 var d = new Date(); var fromatted = d.toLocaleFormat("%d.%m.%Y %H:%M (%a)"); 

But the disadvantage of this is that it is a non-standard function that does not work in Chrome, but works in FF (afaik).

Chris

+1
Aug 02 '13 at 11:12
source share

The above solutions are correct, but may crash in FireFox and Safari! and what webility.js is trying to solve. Check the toUTC function, it works on most major browsers and returns time in ISO format

+1
Aug 21 '16 at 7:12
source share
 /* * convert server time to local time * simbu */ function convertTime(serverdate) { var date = new Date(serverdate); // convert to utc time var toutc = date.toUTCString(); //convert to local time var locdat = new Date(toutc + " UTC"); return locdat; } 
-6
Jan 08 '15 at 8:30
source share



All Articles