I use the following to calculate the difference between two dates in JavaScript:
var dateOne = new Date; // Now
var dateTwo = new Date( dateOne.getTime() + 60 * 1000 ); // Now + One Minute
var difference = new Date( dateTwo - dateOne );
So, logically, difference should be one minute. But Firebug tells me that the difference is one hour off, and the timezone somehow also changes!
dateOne = Date {Sun Sep 11 2011 01:07:55 GMT+0200 (CET)}
dateTwo = Date {Sun Sep 11 2011 01:08:55 GMT+0200 (CET)}
difference = Date {Thu Jan 01 1970 01:01:00 GMT+0100 (CET)}
How can I fix this?
Dateis designed for storing exact dates and times, not differences between dates and times. Subtracting thoseDateobjects yields the number of milliseconds between those two dates. You then create a newDatewith that number of milliseconds since the Epoch. Since the Epoch is midnight of January 1, 1970, the result will be 12:01 AM of January 1, 1970. Daylight savings time changes the timezone a little.