Dayjs: customParseFormat doesn't work when format doesn't fit the Date

Created on 27 May 2019  路  11Comments  路  Source: iamkun/dayjs


Issuehunt badges

dayjs('2019/04/19, 'MMDDYY').isValid()
returns true
and it parses to Wed Jul 20 2005.

Moment.js does not parse it.

https://jsfiddle.net/3meh8g0k/

You can get the same results using other different formats (e.g dayjs('2019/04/19, 'MM' returns Mon May 20 2019)




IssueHunt Summary

iamkun iamkun has been rewarded.

Backers (Total: $100.00)

Submitted pull Requests

- #866 Consider using Intl api and update customParseFormat

Tips

IssueHunt has been backed by the following sponsors. Become a sponsor


Most helpful comment

@iamkun Thanks, but I feel I might have confused you...
I am not proposing to ignore separator tokens, but to require exact matching between input string and format.

 dayjs('2019/04/19, 'YYYYMMDD') ---> invalid
 dayjs('2019/04/19, 'YYYY/MM/DD') ---> 2019/04/19
 dayjs('2019/04/19ab, 'YYYY/MM/DD') ---> invalid, not allowing characters left after parsing

All 11 comments

Bug reproduced, thanks.

@issuehunt has funded $100.00 to this issue.


Since moment.js checked overflow (not allowing month>12, hour>24....), it does not parse ('2019/04/19, 'MM') (not accepting month=20). @iamkun Do you want me to implement the overflow-checking in customParseFormat?

IMHO, it should be more strict when parsing with customParseFormat (moment.js is not strict either). For example,
dayjs('2001/12/01', 'YYYY:MM:DD') // is 2001/12/01
dayjs('2001/12/01', 'YYYY::MM::DD') //invalid
I expect both to be invalid. Without checking the source code, it is hard to understand.

@mirefly thanks, still need some time think about it. Cause we treat dayjs(2019-99-99) as a valid date as well just the same as Javascript new Date(2019, 99, 99)

@iamkun Another approach is to require input and format to match exactly (no extra characters left after parsing all tokens, and ~giving~ requiring delimiter explicitly in format), and allowing 'overflow'. This will invalidate the cases like dayjs('2019/04/19, 'MMDDYY'). I actually like this approach, but not sure if this change is too much.

@mirefly I don't think it's a good idea to ignore all the separator tokens.

@iamkun Thanks, but I feel I might have confused you...
I am not proposing to ignore separator tokens, but to require exact matching between input string and format.

 dayjs('2019/04/19, 'YYYYMMDD') ---> invalid
 dayjs('2019/04/19, 'YYYY/MM/DD') ---> 2019/04/19
 dayjs('2019/04/19ab, 'YYYY/MM/DD') ---> invalid, not allowing characters left after parsing

What about if it is received from different format as well?

Since moment.js checked overflow (not allowing month>12, hour>24....), it does not parse ('2019/04/19, 'MM') (not accepting month=20). @iamkun Do you want me to implement the overflow-checking in customParseFormat?

That would be really useful. The behaviour of vanilla JS Date (which accepts 2019-99-99 as @iamkun wrote) is rather dumb.

@iamkun I think what @mirefly proposing is a good approach. Is there any concern you have?

I'd like to chime in and try implement it :)

@iamkun Thanks, but I feel I might have confused you...
I am not proposing to ignore separator tokens, but to require exact matching between input string and format.

 dayjs('2019/04/19, 'YYYYMMDD') ---> invalid
 dayjs('2019/04/19, 'YYYY/MM/DD') ---> 2019/04/19
 dayjs('2019/04/19ab, 'YYYY/MM/DD') ---> invalid, not allowing characters left after parsing

I also think that @mirefly offers a good solution. Anything new on this issue?

Was this page helpful?
0 / 5 - 0 ratings