dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs fromNow returns Invalid Date

Open jbaan opened this issue 2 years ago • 3 comments

Describe the bug dayjs(NaN).fromNow(false);

returns "a month ago"

Till release 1.11.8 it was always returning "NaN years ago"

Information

Day.js Version 1.11.9 Time zone: GMT+02:00 [MESZ]

jbaan avatar Jul 05 '23 05:07 jbaan

https://runkit.com/embed/iol116brss5g image

iamkun avatar Jul 09 '23 11:07 iamkun

When will this issue be solved?

jbaan avatar Oct 13 '23 06:10 jbaan

Same for undefined value.

dayjs(undefined).fromNow()
// result: 'a month ago'

This is wrong, but the question is, how to handle that. NaN creates a dayjs instance with NaN values inside. But undefined creates a dayjs object like dayjs() with no params. Which should result in 'a few seconds ago' and not a month ago. But dayjs().fromNow() seems to work.

But in any case the developer should check the value before passing it to dayjs. But dayjs fromNow could also handle this in all other ways than retuning wrong text. For example "Invalid date" or "Unknown" or just an empty string?

fudom avatar Apr 02 '24 18:04 fudom