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

CustomParseFormat doesn't parse double digit days and months properly in strict mode #902

Open
jojkos opened this issue May 12, 2020 · 11 comments

Comments

@jojkos
Copy link

jojkos commented May 12, 2020

Describe the bug
When in strict mode, format "D.M.YYYY" should parse both "1.1.2020" and "01.01.2020", but it fails with Invalid date for the second.
https://runkit.com/embed/w5jkq082wagg

moment.js works this way moment/moment#2554
there even seems to be code for this https://github.com/iamkun/dayjs/blob/dev/src/plugin/customParseFormat/index.js (regexes match1to2) but probably doesn't work.

Expected behavior
dayjs("01.02.2020", "D.M.YYYY", true) should be parsed correctly

Information

  • Day.js Version 1.8.26
  • OS: Windows 10
  • Browser Chrome 81.0.4044.129
@iamkun
Copy link
Owner

iamkun commented May 12, 2020

The question comes to is "01.02.2020" an invalid date string to "D.M.YYYY" format?

Since D stands for Day of the month with no leading zero, I tend to agree that 02 is invalid to D in strict mode but valid in loose mode.

@jojkos
Copy link
Author

jojkos commented May 12, 2020

Thanks for the fast reacton.

I would tend to agree as well, if this wasn't supported in moment.js. And since there isn't support for multiple formats #817 the only solution i could come up with is try to parse date with each of the possible formats and find the first that is valid.

@iamkun
Copy link
Owner

iamkun commented May 13, 2020

We can have this, a nice enhancement, right?

@iamkun
Copy link
Owner

iamkun commented May 13, 2020

#906

dayjs('01.02.2020', ['D.M.YYYY', 'DD.MM.YYYY'], true)

@jojkos
Copy link
Author

jojkos commented May 13, 2020

Thanks, this will help.

@geri777
Copy link

geri777 commented Oct 16, 2020

Could this be added for "YY" as well?

@iamkun
Copy link
Owner

iamkun commented Oct 17, 2020

Could this be added for "YY" as well?

you can pass your formats to the second parameter as an array.

@geri777
Copy link

geri777 commented Oct 17, 2020

Could this be added for "YY" as well?

you can pass your formats to the second parameter as an array.

Thank you @iamkun. I know, but the question was if YY could also accept values having YYYY - same as for D and DD.

@iamkun
Copy link
Owner

iamkun commented Oct 17, 2020

@geri777 what I mean is dayjs('2020', ['YYYY', 'YY'], true)

@ArvindhMangoleap
Copy link

@iamkun Hi passing formats in array does not seem to work.
image
Note: month and date values

But passing single format as string is working
image

Am I missing anything?
https://replit.com/@ArvindhAshok/date-Format

@BePo65
Copy link
Contributor

BePo65 commented Jun 2, 2022

Tried it today with dayjs v1.11.2: in both examples I get Day=5 and Month=1.

Which version did you use?

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

5 participants