Javascript Date issue, incorrect timezone

2019-08-03 01:54发布

I got this strange JavaScript bug that I can seem to work arround or fix.

I am using some code to make 2 JavaScript dates, to insert events into a calendar component. The dates are built the following way:

var endDate = new Date();
var startDate = new Date();

startDate.setDate(startDateDay);
startDate.setMonth(startDateMonth);
startDate.setFullYear(startDateYear);
startDate.setHours(2, 0, 0, 0);

endDate.setDate(endDateDay);
endDate.setMonth(endDateMonth);
endDate.setFullYear(endDateYear);
endDate.setHours(2, 0, 0, 0);

So, the dates are built using integers. These integers are determined by input, and using the debugger I can see 100% positive they are coming in correctly. Now, ill describe 3 walkthroughs, 2 where it goes correctly and 1 where it goes wrong.


Using the following input:

endDateDay = 20
endDateMonth = 9
endDateYear = 2014

Gives the following date object as result:

Tue Oct 20 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)

Using this input:

endDateDay = 13
endDateMonth = 9
endDateYear = 2014

Gives the following date object as result:

Tue Oct 13 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)

Now, using this input:

endDateDay = 27
endDateMonth = 9
endDateYear = 2014

Gives the following date object as result:

Mon Oct 27 2014 02:00:00 **GMT+0100** (W. Europe Standard Time)

As you can see, for some strange reason the TimeZone is off. This gives errors in my application, and I need to find a way to get it fixed. Though, I cannot find any solution to it, let alone understand why it is actually happening.

PS: I am using Google Chrome

1条回答
淡お忘
2楼-- · 2019-08-03 02:38

The answer was indeed the difference in the daylight savings time, which I completly oversaw. Thanks to finding this out I also found a solution to my problem.

I used this link to further assist me, might it help someone in the future: http://javascript.about.com/library/bldst.htm

Cheers!

查看更多
登录 后发表回答