Spent a couple of hours tracking down a weird bug with dates in JS, so please allow me to decant some knowledge that resulted from this afternoon's toils.
Some Background Entities in the application have a datetime field and are persisted in the GAE data store. The data store treats all dates/times as being in UTC (as it should, since timezones are part of the presentation layer). I'd already validated that the datetimes of entities in the data store were correctly stored as UTC.
The Problem The application is supposed to display dates as UTC but some were displaying as UTC + 5 (i'm in EST). The UTC time would be accessed through the getUTC* methods on a date object.
The Solution There's a subtlety with the Date constructor (subtle as in i didn't realize it and didn't see it explicitly documented anywhere). Unix timestamps are in GMT and the Date constructor treats them as such. However, date string's without a timezone are treated as in the browser's timezone.
Some examples: new Date("8/5/11 12:11").getHours() // => 12 new Date("8/5/11 12:11").getUTCHours() // => 17
// 1312586961 == Fri, 05 Aug 2011 23:29:21 GMT new Date(1312586961000).getHours() // => 19 new Date(1312586961000).getUTCHours() // => 23
Setting a Date using a unix timestamp will always assume that the timestamp is in UTC. Setting a date using a datestring will assume it's in the user's local time zone if not otherwise specified.
The bug was caused by constructing a Date with a datestring that represented a time in UTC without a timezone in the string. The time was interpreted to be in EST (so getHours was in UTC and getUTCHours was in UTC + 5).
var today = new Date("8/5/11 12:11") // in UTC var tomorrow = new Date(today.getTime() + 86400000) Since it's expected that getTime will return a timestamp in UTC and that today was constructed with a local time it converts that UTC time to UTC again (shifting it forward 5 hours). So tomorrow is actually 1 day & 5 hours ahead.
My problem with these types of arguments is that the proponents want to argue on a group level with some things, but not others. Apparently, all white males are doing something bad by not sharing enou...
Very good article. Does make me thing about these issues from a different angle. <<In the "male" sphere, of professional operations, offices, corporations, pop culture, businesses, the available technology was extremely high-level, better than anywhere I'd yet lived. In the "female" sphere, the home, domestic duties, daily chores, cleaning, heating, anything inside the walls of a house, it was on a level my grandmother would find familiar.>>