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

dayjs constructor does not work with strict flag set to true. #1784

Open
kaujaj opened this issue Jan 27, 2022 · 4 comments
Open

dayjs constructor does not work with strict flag set to true. #1784

kaujaj opened this issue Jan 27, 2022 · 4 comments

Comments

@kaujaj
Copy link

kaujaj commented Jan 27, 2022

Describe the bug

const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
const inputDate = '1993-12-01T10:22:33.128Z';
const isDateValid = dayjs(inputDate, format, true).isValid(); // returns false

another example

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ", true) //doesn't work - returns invalid Date
moment("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ",true) //works

please see #1358 (comment)

Expected behavior
In the first example, isValid() should return true
In the second example, dayjs should behave in the same manner as moment.

Information

  • Day.js Version 1.10.7
  • OS: ubuntu nodejs
  • Time zone: Central Daylight Time -06:00
@odinhb
Copy link

odinhb commented Feb 14, 2022

I'm having the same problem. Even worse though, the fix described in #1358 (comment) gives me the wrong timezone:

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ", true).format() // returns 'Invalid Date'
dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSS[Z]", true).format() // returns '2021-01-26T14:25:04+01:00'
// as far as i understand, both of these should return '2021-01-26T15:25:04+01:00'

dayjs("2021-01-26T14:25:04.871Z").format() // returns '2021-01-26T15:25:04+01:00', which is correct

Environment

  • Day.js version 1.10.4 (Also tried 1.10.5 and 1.10.7, no difference)
  • Chromium 98.0.4758.80 snap on Ubuntu 20.04.3 LTS
  • Timezone: CET (+01:00, as seen above)

@BePo65
Copy link
Contributor

BePo65 commented May 24, 2022

This is similar to issue #1852 only for the "T" character as a legal separator character. As an additional problem the parsing will fail because the logic for 'strict' fails under certain conditions (see issue #1596).

I am preparing a PR for issue #1596 and if this PR will be accepted and merged, I can easily add the necessary modifications required by this issue here.

@BePo65
Copy link
Contributor

BePo65 commented May 26, 2022

Just as kind of a workaround: escaping the 'T' will solve this problem too
dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DD[T]HH:mm:ss.SSSZ", true)

But as this is a very common format, I would prefere a solution with 'T' as an official separator character, identified by dayjs.

@edgarbjorntvedt
Copy link

This seems to be solved now (dayjs@ 1.11.11)

const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
const inputDate = '1993-12-01T10:22:33.128Z';
dayjs(inputDate, format, true).isValid();
// true

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ", true).isValid()
// true

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSSZ", true).format()
// '2021-01-26T15:25:04+01:00'

dayjs("2021-01-26T14:25:04.871Z", "YYYY-MM-DDTHH:mm:ss.SSS[Z]", true).format()
// '2021-01-26T15:25:04+01:00'

dayjs("2021-01-26T14:25:04.871Z").format() 
// '2021-01-26T15:25:04+01:00'

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

4 participants