date-fns: format returns "undefined NaN, 0NaN" for invalid dates
Here is the test case:
https://runkit.com/581c7c34d33aac0014dfc5d3/5847e8805c9e5c001426d5de/branches/master
It should probably use isValid before trying to format and throw in this case?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (14 by maintainers)
Commits related to this issue
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to date-fns/date-fns by leshakoss 7 years ago
- Throw an exception if date isn't valid in `format` (closes #304) (#392) * Throw an exception if date isn't valid in `format` * Add CHANGELOG.md entry — committed to sonmyd/date-fns by leshakoss 7 years ago
@umidbekkarimov @okonet
The point of my proposal is to mirror JavaScript arithmetic operations, with
Invalid Datebeing a counterpart ofNaN(sinceInvalid Dateis aDatewithvalue=NaN)So:
I agree with exception being a result of
format(new Date(NaN))thoughI like the idea of having
Invalid Datein function that returnDateand exception forformat(new Date(NaN)). See the nativeDatebehaviour:Functions like
getSecondsshould always returnNaNwhen the argument is anInvalid Date:It’s works like that right now, but I want to be sure that this is applicable to all similar functions.
I made a research and to make date-fns consistent with
Datewe need to returnNaNfrom the functions that return a number (e.g.getSeconds) and"Invalid Date"from the functions that return a string.@LeshaKoss
You’re right, i made this changes as soon as i jumped to date-fns and
isValidstart throwing type errors everywhere in project 😅 so I double checked it everywhere (yes, I have trust issues)But thanks, i will fix this part in my helpers.
I guess it should throw another
TypeErrorbecause it’s in developer responsibility to check value before parsing it, silently formatting invalid values can create problems in UI@albertorestifo
I disagree with it, i think it’s in developer responsibility to handle unformattable values and libs should not silently return unexpected results
so if it throw an error you can handle it like that:
Otherwise you should do things like I did in https://github.com/date-fns/date-fns/issues/304#issuecomment-265673346 example
Use
isValideverywhere and returnInvalid Datewould reflect the same behaviour asDate, seems like a good idea to me.It would certainly give me more clues about what’s going on that seeing
undefined NaN, 0NaNsomewhere in my app