-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(diff): allow .diff(unit) shorthand for diffing with current time #2945
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
Conversation
|
👌 |
|
@iamkun this is pretty small |
|
Thanks for the contribution! Really appreciate the effort to make the API feel more intuitive. After checking the behavior in Moment.js, it turns out that Moment does not support this shorthand syntax either — you always need to provide a comparison date as the first argument to moment('2025-01-10').diff('days') // invalid → tries to parse "days" as a date
moment('2025-01-10').diff(moment(), 'days') // ✅ valid usageSo in order to stay consistent with Moment.js (and to avoid unexpected implicit behavior), maybe, Day.js has also required a comparison date to be passed explicitly. Could you share which specific part of that documentation gave you this impression? |
|
Thank you for the feedback, didn't realise MomentJS was still being used as a benchmark I think the lack of compile time error must have made the issue creator confused, we can mark this PR as closed |
|
You can actually see that most of the Day.js unit tests use Moment.js results as a reference — a lot of community members also adopt Day.js as a drop-in replacement for Moment.js. At the same time, I think we could improve the developer experience by adding an error or warning for this specific case in the https://day.js.org/docs/en/plugin/dev-helper dev helper plugin. What do you think about that approach? |
|
I see |
|
Really excited for your PR! Appreciate you taking this on. 🙌 |
|
Just created a PR for that in #2948 , happy to close this afterwards |
|
closed as addressed, solution available in #2948 |
Summary
This PR adds shorthand support for calling
.diff(unit)as equivalent to.diff(dayjs(), unit).Problem
The documentation suggests that if no date is supplied,
.diff()compares with the current time.However, calling
.diff('days')or.diff('hours')currently treats the string as a date, resulting inInvalid Date→NaN.Solution
.diff()is called with a single recognized unit string (e.g. 'days').Example
Resolves #2943