Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date.prototype.setMonth() changes the timezone offset?

Tags:

javascript

This only occurs with the setMonth() method and none of the other set*() methods.

Here's an example of what I mean:

var date = new Date();
date.getTimezoneOffset(); // -60

date.setMonth(5);
date.getTimezoneOffset(); // -120

Is this expected behavior? There's nothing mentioned about this on MDN.

like image 958
Alternatex Avatar asked Oct 22 '25 03:10

Alternatex


1 Answers

The getTimezoneOffset method returns a different value during DST, and in month 5 the DST is different than now.

The time zone offset returned is the one that applies for the Date that it's called on. Where the host system is configured for daylight saving, the offset will change depending on the date and time that the Date represents and that daylight saving applies.

var date = new Date();
console.log(date.getTimezoneOffset());

date.setMonth(0);
console.log(date.getTimezoneOffset());

date.setMonth(5);
console.log(date.getTimezoneOffset());

btw, if you will run this code during June - you will see that there is no difference :)

like image 160
Dekel Avatar answered Oct 23 '25 17:10

Dekel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!