Convert UTC dates to local timezone offset automatically 4

Posted by Nick Ashley on September 11, 2007

So I was working on an application that printed out lots of dates to the user. However, I get traffic from all over the world, and as this is kind-of time sensitive information, I wanted to display the time in their own timezone. I have don’t this before on things like forums, where you allow the user to select their timezone when they register. But what if your users don’t register?

I decided to build a javascript function that would automatically convert from UTC time to the users’ local timezone. This is a pretty simple task, and should work very effectively. I already store the dates in my database as UTC format, which is a good idea to do so that there is never any confusion later as to what timezone a date is in (What if you and/or your server moves?). Also because this is a javascript function, we have the ability to determine the users local time - something that we normally wouldn’t have.

Without further ado, here is the code:


var dateFunction = {
  convertDate : function (gmtDate){
    var originalDate = new Date(gmtDate);
    var retStr = (originalDate.getMonth()+1) + "-" + originalDate.getDate() + "-" + originalDate.getFullYear();
    return retStr;
  },
  init : function(){
    var elems = YAHOO.util.Dom.getElementsByClassName('utcDate');
    for(var i=0;i<elems.length;i++)
    {
      elems[i].innerHTML = dateFunction.convertDate(elems[i].innerHTML);
    }

  }
}
YAHOO.util.Event.addListener(window, "load", dateFunction.init);

As you can see, I am using the YUI library to do some DOM parsing, and unobtrusively add some event listeners. The code should be pretty straight forward, when the page loads I look for all elements with a class of “utcDate” and pass the innerHTML into a separate function. The other function create a new date object, using the original UTC date as the time. By doing this, javascript automatically will display this new date in the users’ local time. I can then return the date in whatever format I want. Finally, I replace the innerHTML of the element with the new date string, and we are all set

This provides a simple way to convert dates to local timezone offset, and all the is required is for you to wrap any date you want converted in an element with a class equal to “utcDate”. It also degrades gracefully, because without javascript users will simply see the date in UTC format, which isn’t really a bad thing, and probably what you would be showing them anyways, if you didn’t have this nifty script!

Related Articles

  1. Google adds ‘Suggest’ feature to homepage
  2. Javascript form validation object
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Nic Oct 01, 2008 16:28

    This doesn’t convert UTC to local time. All it does is reformat your date to Day-Month-Year

  2. Nick Ashley Oct 26, 2008 11:39

    Actually, it does. I agree that It doesn’t look like it is going anything other then reformatting the date. However, Javascripts Date object automatically returns information in the user’s own timezone. Because javascript is executed client side, it has this information available to it.

  3. Kevin Jul 03, 2009 14:19

    Actually it doesn’t. Some versions of IE will ignore the X in new Date(X)

  4. Nick Ashley Aug 07, 2009 02:12

    Kevin,

    Which versions of Internet Explorer ignore the parameter?

Comments

Comments: