Farhad Azarbarzinniaz
Farhad Azarbarzinniaz

Reputation: 739

Wrong date value in firefox

different behavior for JavaScript Date in Browsers.

I create new Date in Firefox But it's return incorrect value .

in Firefox:

new Date(2017,2,22)
Date 2017-03-21 T19:30:00.000Z

in Chrome:

new Date(2017,2,22)
Wed Mar 22 2017 01:00:00 GMT+0430 (Iran Daylight Time)

how to make Firefox act date like chrome?

Upvotes: 0

Views: 786

Answers (1)

RobG
RobG

Reputation: 147363

Your issue seems to be with how browsers apply daylight saving time. In Tehran, clocks move forward for daylight saving at midnight at the start of 22 March, 2017. So 2017-03-22 00:00:00 instantly becomes 2017-03-22 01:00:00.

It seems that Firefox does not apply daylight saving to exactly midnight, whereas Chrome does. It seems to apply the wrong offset (it actually subtracts an hour from standard time) until 01:00:

new Date(2017,2,22,0,59); // Tue Mar 21 2017 23:59:00 GMT+0330 (IRST)
new Date(2017,2,22,1,0);  // Wed Mar 22 2017 01:00:00 GMT+0430 (IRST)

And it uses the same time zone name abbreviation for both. Report it as a bug.

There are many minor such issues with browser Date objects. If you depend on client Date behaviour, be prepared to discover them.

Edit

It seems you can work around this using Date.UTC:

// One minute to midnight in Tehran, daylight saving not applied
new Date(Date.UTC(2017,2,21,20,29)); // Tue Mar 21 2017 23:59:00 GMT+0330 (IRST)
// At midnight in Tehran, daylight saving applied
new Date(Date.UTC(2017,2,21,20,30)); // Wed Mar 22 2017 01:00:00 GMT+0430 (IRST)

which still has the incorrect timezone abbreviation but you should never rely on that anyway, the rest seems correct.

Upvotes: 2

Related Questions