Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect date parsing in Firefox browser #1547

Closed
ppiyush13 opened this issue Jun 22, 2021 · 2 comments
Closed

Incorrect date parsing in Firefox browser #1547

ppiyush13 opened this issue Jun 22, 2021 · 2 comments

Comments

@ppiyush13
Copy link

Describe the bug
Parsing date using dayJs and then converting to native JavaScript Date is not working as expected in Firefox browser.

const parsedDate = dayJs(
  "25-MAY-2021 12:00 +08:00",
  "DD-MMM-YYYY HH:mm Z"
).toDate().toString();

Output
Getting below output in Firefox browser

Fri May 25 -2021 09:53:28 GMT+0553 (India Standard Time) 

Expected output
In chrome and NodeJs, getting correct below output

Tue May 25 2021 09:30:00 GMT+0530 (India Standard Time)

Issues:

  • Time is incorrect (09:53:28 instead of 09:30:00)
  • Time zone is incorrect (GMT+0553 instead of GMT+0530)
  • Year is negative (-2021 instead of 2021)
  • Weekday is incorrect (Friday instead of Tuesday)

Could this be related to Firefox bug Inconsistent date object - Negative year in result ?

Information

  • Day.js Version: 1.10.5
  • OS: windows 10
  • Browser: Firefox 89.0.1 (64-bit)
  • Time zone: GMT+05:30 IST (India Standard Time)

Issue reproduce
I have created minimal repo to reproduce above issues https://codesandbox.io/s/determined-mayer-fsc4e?file=/src/index.js

Anyone aware of possible workarounds ?

@MirzaLeka
Copy link

MirzaLeka commented Jul 26, 2021

I've noticed the same issue with Moment on Firefox. Scary

import * as moment from 'moment'

moment(
    "25-MAY-2021 12:00"
  ).format("YYYY-MM-dddd HH:mm:ss")

// chrome: "2021-05-Tuesday 12:00:00"
// firefox: "-2021-05-Friday 12:00:00"

Anyway, there is a workaround with the customParseFormat plugin:

import * as customParseFormat from 'dayjs/plugin/customParseFormat'
import * as dayjs from 'dayjs'

dayjs.extend(customParseFormat)
const date = dayjs('2021-05-25 12:00 PM', 'YYYY-MM-DD HH:mm A').toDate().toString()

// "Tue May 25 2021 12:00:00 GMT+0200 (Central European Summer Time)"

and the output is the same across browsers. More on that here.
Cheers!

@iamkun
Copy link
Owner

iamkun commented Jul 27, 2021

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

ref https://day.js.org/docs/en/parse/string

@iamkun iamkun closed this as completed Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants