date-fns: Invalid time value
i use the latest version ( i.e. date-fns v2.0.0 )
if i run this stuff : format(new Date(), 'yyyy-mm-dd')
i got this error:
Starting with v2.0.0-beta.1 date-fns doesn't accept strings as arguments. Please use `parseISO` to parse strings.
so i change to use format(parseISO(new Date()), 'yyyy-mm-dd')
but it still not work
is’t a bug ?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 18 (3 by maintainers)
Kinda irrelevant at this point, but at least maybe I will see my comment if I forget this again.
new Date(undefined)returnsInvalid Datewhich, when passed to format, will result inRangeError: Invalid time value. The reason this is somewhat surprising is because I expectednew Date(undefined)to be equivalent tonew Date()since missing parameters default toundefined. However, if the Date constructor checks arity with something likeconstructor(...args) { if (args.length > 0) { ... } }I can see how this could cause an issue.Hi, @kossnocorp, thx reply
this works :
so looks like i use the wrong import method
@hereiscasio arguments order in FP functions is inverted, so you have to:
…but:
Perhaps add this condition to make sure you aren’t calling new Date(undefined).
post.last_publication_date !== undefined@sujayjaju you can’t pass strings to
toDate: https://github.com/date-fns/date-fns/blob/master/src/toDate/index.js#L53-L57If you want to parse a loose date string, use
new Date('2020-05-18 11:11')the following helped me:
format(parseISO(user.createdAt), 'dd.MM.yyyy; HH:mm')@jonasgroendahl I used the new Date as suggested by @kossnocorp above