Okay, say JSON parse string UTC date as 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?
How do I format it to something else like yyyy-MM-dd HH:mm:ss z
?
This date.toString('yyyy-MM-dd HH:mm:ss z');
never work out :/
Examples of how to convert UTC to your local time To convert 18:00 UTC (6:00 p.m.) into your local time, subtract 6 hours, to get 12 noon CST. During daylight saving (summer) time, you would only subtract 5 hours, so 18:00 UTC would convert to 1:00 p.m CDT. Note that the U.S. uses a 12-hour format with a.m. and p.m.
Use the Date() constructor to convert UTC to local time, e.g. new Date(utcDateStr) . Passing a date and time string in ISO 8601 format to the Date() constructor converts the UTC date and time to local time. Copied!
Answer: Use the toString() Method You can simply use the toString() method to convert UTC or GMT date time to local date time in JavaScript.
The Date. 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.
Try:
var date = new Date('2012-11-29 17:00:34 UTC'); date.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With