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

InvalidDate using customParseFormat with strict mode #1561

Open
lsorin77 opened this issue Jul 1, 2021 · 2 comments
Open

InvalidDate using customParseFormat with strict mode #1561

lsorin77 opened this issue Jul 1, 2021 · 2 comments

Comments

@lsorin77
Copy link

lsorin77 commented Jul 1, 2021

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
@lsorin77 lsorin77 changed the title InvalidDate parsing format 'h:mm:ss.SSS A' with strict=true InvalidDate using customParseFormat with strict mode Jul 1, 2021
@whlee0525
Copy link

whlee0525 commented Nov 18, 2021

EX 1)
It looks like EX1 is Invalid because there is no matched format.
There should be HH:mm:ss.SSS A.

a. console.log("without meridiem ", dayjs('04:59:00.900','HH:mm:ss.SSS',true).format('h:mm:ss.SSS A')); //Valid date
b. console.log("with meridiem ", dayjs('04:59:00.900 PM','h:mm:ss.SSS A',true)).format('h:mm:ss.SSS A')); //InvalidDate

In b) case, meridiem is not the problem.
Your input(04:59:00.900) is same, but format(a - HH, b - h) is different.
'h:mm:ss.SSS A` should be 'HH:mm:ss.SSS A'

EX 2)
I'm not sure, but I just want to share my case.
I'm in time zone Z is +09:00.
If I change '2021-06-30T16:55+00:00', to '2021-06-30T16:55+09:00',,
it returns ValidDate by format 'YYYY-MM-DD[T]HH:mmZ'. It would be good to check your time zone.

@BePo65
Copy link
Contributor

BePo65 commented May 28, 2022

Example 1 fails, because the best matching format ('h:mm:ss.SSS A') does not have a 2 digit hour part, but the input has. Therefore the hour does not match, but we parse in strict mode.
Besides that, there is no format part 'SSSSSSS' in dayjs (neither in parsing nor in formatting); therefore the format 'HH:mm:ss.SSSSSSS' would fail, even if you added a meridiem part ('A').

Example 2 fails, because dayjs fails in strict mode, when the parsed date has an offset different from the current time zone. This would be fixed by pr #1914.
The example dayjs('04:59:00.900 PM','h:mm:ss.SSS A',true)).format('h:mm:ss.SSS A')) fails because the parsing format requires a single digit hour ('h'), but the input has a double digit hour ('04') and in strict mode leading zeros are not ignored.

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

3 participants