0

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?

  • Read this answer. http://stackoverflow.com/a/15110385/4594699 – Ashhar Hasan Jan 07 '17 at 06:09
  • 1
    Possible duplicate of [new Date() works differently in Chrome and Firefox](http://stackoverflow.com/questions/15109894/new-date-works-differently-in-chrome-and-firefox) – Ashhar Hasan Jan 07 '17 at 06:10
  • @AshharHasan that question you mentioned not helped me i already read it . in Firefox when i create even correct date with correct format , after i try to use date.setFullYear it's return to wrong date . – Farhad Azarbarzinniaz Jan 07 '17 at 06:25
  • Basically if time zone is missing from Date, FF assumes it to be local time while Chrome assumes it to be UTC. The short answer is that the Date.Parse() is implementation defined and hence differs between browsers. If you add time zone info you will get the same date. – Ashhar Hasan Jan 07 '17 at 06:29
  • 1
    @AshharHasan—there is no parsing here, the OP is using the Date constructor with no parsing. – RobG Jan 07 '17 at 07:08

1 Answers1

2

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.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • thanks.I use this Date in Telerik scheduler and i have to get correct date in client side . so the one option is to use UTC but it's work incorrect – Farhad Azarbarzinniaz Jan 07 '17 at 07:54
  • this question explain more http://stackoverflow.com/questions/28989484/eliminating-javascript-daylight-saving-time-gap-a-cross-browser-solution – Farhad Azarbarzinniaz Jan 07 '17 at 08:25
  • also this one http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – Farhad Azarbarzinniaz Jan 07 '17 at 08:27
  • this serach query will be very help full https://www.google.com/search?q=how+browsers+apply+daylight+saving+time&oq=how+browsers+apply+daylight+saving+time&aqs=chrome..69i57&sourceid=chrome&ie=UTF-8 – Farhad Azarbarzinniaz Jan 07 '17 at 08:35
  • @farhadazarbarzinniaz—the first link just uses UTC (via moment.js) which is essentially the same as using *Date.UTC*. You should do everything in UTC and only use local for display. A library that works with timezones will help with that. – RobG Jan 07 '17 at 08:58