Localizing time from UTC dates

123 views
Skip to first unread message

Kamran

unread,
Dec 21, 2009, 3:44:58 PM12/21/09
to Datejs - A JavaScript Date Library
Hey all,

My localizing function works pretty well. It parses a UTC date (given
by my database) on the page and uses jQuery to replace the text with a
local time. I successfully get the UTC offset hours and add it to the
UTC date to get the local time.

The problem I am having is that the toString(format) method is not
formatting the hours correctly.

Here is the date from the database:

12/21/2009 6:34:28 AM

I am in the CST timezone, so my offset is -6. Well, if I add -6 hours
to that, I get 0:30:28 AM, which is really 12:30:28 AM. However,
datejs doesn't seem to understand that and outputs this:

12/21/09 0:34 AM

Any ideas?

Here is my function:

/**
* Localize UTC dates to client time
*/
function localizeDates() {
$(".utc-date").each(function() {

var theDate = Date.parse($(this).text());
var theOffset = Date.today().getUTCHours(); // in hours (e.g. 6)

if (theDate) {
theDate.addHours(-theOffset);

$(this).text(theDate.toString("M/d/yy h:mm tt"));
}
});
}

$(localizeDates);

Reply all
Reply to author
Forward
0 new messages