iamkun / iamkun/dayjs

customParseFormat strict mode bug

Open
#929 18 comments 14 reactions 0 assignees View on GitHub
☢️Bug
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**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:
```javascript
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:
```javascript
{
"$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:
```javascript
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:
```javascript
"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)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.