How To Convert JavaScript Local Date to UTC And UTC To Local Date

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)

January 02, 2012 22:00:00 GMT+0530



How to convert the date into a local time(CST)?

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.

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 –

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?

now.toUTCString()
//now = Mon, 02 Jan 2012 16:30:00 GMT



How to get the local date from the UTC date?

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.

var millis = now.getTime() + (now.getTimezoneOffset() * 60000)
//millis = 1325543400000



How to convert the milliseconds in UTC to local date? Subtract the timezone offset.

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.

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

This entry was posted in JavaScript and tagged , , , , , , . Bookmark the permalink.

10 Responses to How To Convert JavaScript Local Date to UTC And UTC To Local Date

  1. JanB-Punkt says:

    Hi Lobo,

    i’m really new to JS and i wonder how to convert a given date and time to a UTC timestamp.
    in fact, i need the timestamp of that given date/time because i want to substract it from “now”. then i want to build a new date() from the result of this substraction.
    the thing ist, that all of these steps should be in UTC so i can run it on every pc in every time zone.


    var start = new Date(2011, 6, 1, 3, 24, 23);
    var now = new Date();
    var diff = new Date(now - start);

    help me please :)

    Thank you!
    Jan

  2. qiller says:

    it’s an old post, but since people may still stumble upon it… isn’t it not entirely correct when converting local time to UTC timestamp and back?

    > var millis = now.getTime() + (now.getTimezoneOffset() * 60000)

    by js standard, getTime() will already return time in UTC…

  3. What about DayLightSaving?

  4. Kathy says:

    Thanks for your post, it is helping me a lot. A question how can I handling DayLightSaving with these timezone function. I should need to handle the same in my code. Please advice.

  5. Awesome post. Appreciate your hardwork

    I have an issue with date in my project. From Back end java code, i am sending date to front end,

    In front end i am comparing current client browser time to back end date,

    For this i am having lot of issues.

    so i tried to do both back end and front end dates as UTC date

    My Back end date is ‘Tue May 21 2013 07:01:41′

    and Front end date in js is ‘Tue, 21 May 2013 06:57:39 GMT’ using now.toUTCString() function.

    How can i remove GMT in front end using java script?

    Thanks Kiran

    • Luis Alberto says:

      @kiran I have the same problem and wasn’t able to find a solution yet. When I use toUTCString(), it adds a comma and returns a different string. That is not accepted in jquery plugin.

      Have you found anything?

    • Luis Alberto says:

      heeeeeeeeeeeeey!!

      I just got it working. Just when I was about to give up..

      function getLocalTimeFromGMT(sTime){
      var mydate = new Date(sTime);
      dte.setTime( mydate.getTime() + mydate.getTimezoneOffset()*60*1000 );
      return mydate
      }

  6. Teo says:

    @Kiran, i had this problem too when i made my site where i wanted to be able to convert milliseconds to time and date. If you take a look in the script: instead of using a single toString() function on the date i concatenated 2 strings: date.toDateString()+’ ‘+date.toLocaleTimeString(). This will return the date and time in the format your back end date is in.

  7. Armen Kirakosyan says:

    hi
    thanks for the article.

    One question: How to get date time in UTC with milliseconds ???

Leave a Reply

Your email address will not be published. Required fields are marked *


5 − = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.