dayjs constructor does not work with strict flag set to true.
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 https://github.com/iamkun/dayjs/issues/1358#issuecomment-774296660
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
I'm having the same problem. Even worse though, the fix described in https://github.com/iamkun/dayjs/issues/1358#issuecomment-774296660 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 snaponUbuntu 20.04.3 LTS - Timezone: CET (+01:00, as seen above)
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.
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.
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'