dayjs: customParseFormat strict mode bug
Describe the bug Using the format string “YYYY-MM-DD HH:mm:ss ZZ”, parsing certain strings returns Invalid Date when it should return a date:
var format = "YYYY-MM-DD HH:mm:ss ZZ";
var ok = dayjs("2018-03-01 00:01:00 +0000", format, true);
var bad = dayjs("2018-04-01 00:01:00 +0000", format, true);
where bad
is:
{
"$D": 1
"$H": 1
"$L": "en"
"$M": 3
"$W": 0
"$d": Invalid Date
"$m": 1
"$ms": 0
"$s": 0
"$u": undefined
"$y": 2018
}
The parsing is successful if strict mode is not used. The problem seems to be in the customParseFormat.js
line:
if (isStrict && date !== this.format(format)) {
where the date that the plugin has parsed is not matching the default output from dayjs.format()
. In the above case:
"2018-04-01 00:01:00 +0000" !== "2018-04-01 01:01:00 +0100"
but the same bug will effect parsing any date where the timezone doesn’t match whatever local timezone/dst dayjs uses. eg. “2018-03-01 00:01:00 +0600”, “2018-04-01 00:01:00 -0200”, etc.
Expected behavior
bad
should be a valid date object.
Information
- Day.js Version: v1.8.28
- OS: Windows 7
- Browser: Firefox 77, Chrome 83
- Time zone: UTC+00:00 DST (British Summer Time)
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 11
- Comments: 15 (1 by maintainers)
Commits related to this issue
- fix: customParseFormat strict mode bug when parsing with timezones (#929) — committed to ivansieder/dayjs by ivansieder 3 years ago
Bug confirmed. Strict mode does not work good with parsing timezone (ZZ)
I’m looking for a way to solve it.
@iamkun is there anything else we can do to help get this pull request merged?
any news about the problem?
We have the same problem. I created this codesandbox example to reproduce the issue
any news about the timezone parsing issue? It will be great to use it properly as I’m migrating from
luxon
todayjs
Is there any progress for this?
@iamkun Just a friendly reminder 😃
@BreakBB thank you for the hint. I’ve merged it now with dev, updated the code and the tests are passing. Now waiting for further feedback from @iamkun 👍