iamkun / iamkun/dayjs

InvalidDate using customParseFormat with strict mode

Open
#1,561 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Example #1:
const time = dayjs('04:59:00.900 PM', ['HH:mm:ss.SSSSSSS', 'HH:mm:ss', 'HH:mm', 'h:mm A', 'h:mm:ss A', 'h:mm:ss.SSS A'], true); // InvalidDate
console.log(time.format('HH:mm:ss.SSSSSSSSS')); //InvalidDate

Example #2:
const parsed = dayjs(
'2021-06-30T16:55+00:00',
[
'MM-DD-YYYY h:mm A',
'MM-DD-YYYY h:mm:ss A',
'MM-DD-YYYY h:mm:ss.SSSSSSSSS A',
'YYYY-MM-DD[T]HH:mm:ss.SSSSSSSSSZ',
'YYYY-MM-DD[T]HH:mm:ssZ',
'YYYY-MM-DD[T]HH:mmZ',
],
true,
); //InvalidDate
console.log(parsed.isValid); //returns false

**Expected behavior**
Both examples should return a valid date like moment does.
Formatting #1 should return '16:59:00.900000000'

It seems that #2 has an issue with the meridiem since the following works:
console.log("without meridiem ", dayjs('04:59:00.900','HH:mm:ss.SSS',true).format('h:mm:ss.SSS A')); //Valid date
console.log("with meridiem ", dayjs('04:59:00.900 PM','h:mm:ss.SSS A',true)).format('h:mm:ss.SSS A')); //InvalidDate

- Day.js Version 1.10.5
- Windows 10
- Chrome Version 91.0.4472.124
- Time zone: CST

Contributor guide

Open the contributing guide

Research direction

Start with the customParseFormat plugin and reproduce both strict-mode examples from the issue, including the meridiem and fractional-second formats. Trace why these inputs become InvalidDate, then verify that both examples parse successfully and that the first formats as 16:59:00.900000000.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.