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
differenceInSeconds() returns -0 when comparing the same date with millisecond differences #2555
Comments
I wouldn't mind date-fns returning -0 in the What about |
…#2555) Added `roundingMethod` option to `differenceInHours`, `differenceInMinutes`, `differenceInQuarters`, `differenceInSeconds` and `differenceInWeeks` with `trunc` as the default method. Co-authored-by: Lucas Silva <lucas.silva@codeminer42.com> Co-authored-by: Tetiana <ttobin@protonmail.ch> Co-authored-by: Sasha Koss <koss@nocorp.me>
closes date-fns#2555) Added `roundingMethod` option to `differenceInHours`, `differenceInMinutes`, `differenceInQuarters`, `differenceInSeconds` and `differenceInWeeks` with `trunc` as the default method. Co-authored-by: Lucas Silva <lucas.silva@codeminer42.com> Co-authored-by: Tetiana <ttobin@protonmail.ch> Co-authored-by: Sasha Koss <koss@nocorp.me>
I was hit with this issue. Maybe another regression or a different scenario.
Results in:
The issue happens when the |
A PR is welcome! The easiest solution is |
Fixed and shipped with v3.3.0: https://github.com/date-fns/date-fns/releases/tag/v3.3.0 |
Thank you @kossnocorp FWIW I missed your last update otherwise I'd be glad to git it a shot. The reason for the For instance, you can try something like this in any console: console.log(Math.round(0.001 - 0.002))
-0 So effectively, any routine that rounds or ceils fractions can have this issue. As for a solution, the workaround I applied to my application as adding a zero: console.log(Math.round(0.001 - 0.002) + 0)
0 Basically adding |
Thanks @kossnocorp, any chance of the negative number fixes in v3.3.0 could be backported to v2? Unfortunately I am unable to upgrade to v3, all good if it is too much of an ask, I have fixed it manually, but assume it probably affects a few others as well. |
I have tested this with the latest version
2.22.1
. There is an older issue and test to prevent the negative 0, so maybe this is a regression?https://github.com/date-fns/date-fns/blob/master/src/differenceInSeconds/index.ts#L36
For the above 2 dates, the
differenceInMilliseconds
is-0.003
andMath.ceil(-0.003)
is-0
.The text was updated successfully, but these errors were encountered: