Wednesday, August 13, 2014

How To Convert JavaScript Local Date to UTC And UTC To Local Datelearn(n*share(n,learn(n+1)))

How To Convert JavaScript Local Date to UTC And UTC To Local Datelearn(n*share(n,learn(n+1)))





DST is such a pain when it comes to programming. I wish they just get rid of it. It’d be helpful, however, the programmers will still have to deal with the timezones. I was answering questions related to the timers on this website and I keep getting a lot of questions on DST and the timezones. Even though it seems easy, this topic is very confusing. There are lot many sources on the Internet and reading them confuses the hell out of me.
To avoid conflicts when dealing with the transactions from many different timezones, it is essential to normalize the dates and by normalizing I mean converting it to UTC. Let me take an example in JavaScript.
Let the date in question be in Indian Standard Time (IST)
1
January 02, 2012 22:00:00 GMT+0530


How to convert the date into a local time(CST)?
1
2
var now = new Date("January 02, 2012 22:00:00 GMT+0530");
// now = Mon Jan 02 2012 10:30:00 GMT-0600 (CST)


How to convert a date to UTC? This is where many of the online sources go wrong. The simple answer is to convert the date into milliseconds since Epoch and add the timezone offset and convert it back to a date object. Simple? No, this is partially correct. When the calculated milliseconds are converted back to a date, you get a local date.
1
2
var nowUtc = new Date( now.getTime() + (now.getTimezoneOffset() * 60000));
//nowUtc = Mon Jan 02 2012 16:30:00 GMT-0600 (CST)


Notice the GMT-0600 (CST) part at the end. That means the resulting date is not GMT, it is CST. If you convert this date to GMT it will read – Mon Jan 02 2012 22:30:00 GMT but we want it to be Mon Jan 02 2012 16:30:00 GMT.
There is also another incorrect way mentioned all over the Internet –
1
var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());


The result will be no different than the one that you see above.
How to convert the local date to UTC date?
1
2
now.toUTCString()
//now = Mon, 02 Jan 2012 16:30:00 GMT


How to get the local date from the UTC date?
1
2
now = new Date(now.toUTCString());
//now = Mon Jan 02 2012 10:30:00 GMT-0600 (CST)


That is a long string to store, is there any alternative to store the UTC time? Just store the number of milliseconds since Epoch converted to UTC by adding the timezone offset.
1
2
var millis = now.getTime() + (now.getTimezoneOffset() * 60000)
//millis = 1325543400000


How to convert the milliseconds in UTC to local date? Subtract the timezone offset.
1
2
now.setTime(millis - (now.getTimezoneOffset() * 60000))
//now = Mon Jan 02 2012 10:30:00 GMT-0600 (CST)


Let me know if you have any questions or comments.

I just used the below HTML to test the above mentioned code. If you wish, create an HTML file out of it and open it in a browser.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
<body>
<script type="text/javascript">
document.write("IST time - January 02, 2012 22:00:00 GMT+0530");
 
var now = new Date("January 02, 2012 22:00:00 GMT+0530");
document.write("<br/>IST converted to local time: " + now);
 
var nowUtc = new Date( now.getTime() + (now.getTimezoneOffset() * 60000));
document.write("<br/>Local time converted to UTC:" + nowUtc);
 
nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
document.write("<br/>Local time converted to UTC:" + nowUtc);
 
document.write("<br/>Local to GMT " + now.toUTCString());
 
document.write("<br/>GMT to Local " + new Date(now.toUTCString()));
 
var millis = (now.getTime() + (now.getTimezoneOffset() * 60000));
document.write("<br/>GMT in millis " + millis);
 
document.write("<br/>Local in millis " + ( millis - (now.getTimezoneOffset() * 60000)));
 
now.setTime(( millis - (now.getTimezoneOffset() * 60000)));
document.write("<br/>Local from millis " + now);
</script>
</body>
</html>
This code prints the following –
IST time – January 02, 2012 22:00:00 GMT+0530
IST converted to local time: Mon Jan 02 2012 10:30:00 GMT-0600 (CST)
Local time converted to UTC:Mon Jan 02 2012 16:30:00 GMT-0600 (CST)
Local time converted to UTC:Mon Jan 02 2012 16:30:00 GMT-0600 (CST)
Local to GMT Mon, 02 Jan 2012 16:30:00 GMT
GMT to Local Mon Jan 02 2012 10:30:00 GMT-0600 (CST)
GMT in millis 1325543400000
Local in millis 1325521800000
Local from millis Mon Jan 02 2012 10:30:00 GMT-0600 (CST)

No comments: