spaghetticowboy
spaghetticowboy

Reputation: 708

javascript timezone weirdness: new Date() creating dates in different timezones

I have some javascript that is acting weird. I create two dates, but they are randomly in different timezones which is messing me up when calculating the difference between the two. I am in the EST timezone, but for some reason the first date get created as a EDT date.

Basically my question is why does this javascript produce the output that it does?

var xstartTime = new Date(startYear, StartMonth, StartDay, StartHour, StartMinute, 0, 0);
var xendTime = new Date(EndYear, EndMonth, EndDay, EndHour, EndMinute, 0, 0);
console.log("StartMinute" + StartMinute + "StartHour" + StartHour + "StartDay" + StartDay + "StartMonth" + StartMonth + "startYear" + startYear);
console.log(xstartTime);
console.log("EndMinute" + EndMinute + "EndHour" + EndHour + "EndDay" + EndDay + "EndMonth" + EndMonth + "EndYear" + EndYear);
console.log(xendTime);

StartMinute0StartHour0StartDay3StartMonth10startYear2013

Sun Nov 3 00:00:00 EDT 2013

EndMinute59EndHour23EndDay3EndMonth10EndYear2013

Sun Nov 3 23:59:00 EST 2013

Upvotes: 1

Views: 240

Answers (1)

Jay Nebhwani
Jay Nebhwani

Reputation: 976

It's not a JavaScript problem, it's related to timezones. Taken from this Wikipedia article:

During the first Sunday in November, at 2:00 a.m. EDT, clocks are moved back to 1:00 a.m. EST, thus "duplicating" one hour.

Your date is coincidentally the first Sunday of November 2013. xstartTime is 00:00 EDT but since xendTime is after 1:00 am, its timezone is EST.

Upvotes: 3

Related Questions