-
Notifications
You must be signed in to change notification settings - Fork 7.1k
moment().diff(moment()) does not always return 0 #5195
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
Comments
The inconsistency is due to the two i.e. In your case, the // this might return exactly 366, or something like 365.9999999768518
moment().add(1, 'years').diff(moment(), 'days', true); |
@ashsearle Would it be appropriate to document this behaviour? I spent the better part of a day debugging this as it wasn't apparent (to me, anyway) that two |
I'm not sure. I know you're not the only person to get tripped up by this, but it's no different to being surprised that Let's suppose it was documented: did you look for the documentation? Where did you look? |
I read the docs for Now, as well as Difference. In my opinion, it would make sense to note that initializing multiple moments in the same line of code won't guarantee identical As I've been a JS dev for about 10 years and this behaviour surprised me, I think it's reasonable to assume this isn't common knowledge? Part of the appeal of If you agree that this should be documented, I can work on a PR soon. I'm obviously open to feedback on where it should go 🙂 |
I agree the Now section feels like the right place to put it. Let's talk about the example in that section: var x = undefined;
moment(x).isSame(moment(), 'second'); // true I think it's written that way to avoid implying However that's a bad way to do things as it's occasionally going to act like: var a = moment();
var b = a.clone().add(1, 'millisecond');
a.isSame(b, 'second'); // false if ms wrapped rolled over from 999 to 0 It would probably be better to change that example to a |
Agree with @ashsearle! Otherwise the below code returns random results depending on current time.
|
Describe the bug
When repeatedly running this calculation:
moment().add(1, 'years').diff(moment(), 'days')
when the current date is in a year before a leap year (in my case, 2019/08/13), moment inconsistently returns365
or366
. Adding.startOf('day')
to eachmoment()
will consistently return366
.To Reproduce
Steps to reproduce the behavior:
moment().add(1, 'years').diff(moment(), 'days')
at least 4 times.365
and366
inconsistently.moment().startOf('day').add(1, 'years').diff(moment().startOf('day'), 'days')
at least 4 times.366
each time.Expected behavior
moment should consistently account for leap years when calculating day differences.
Desktop (please complete the following information):
Moment-specific environment
Please run the following code in your environment and include the output:
Output:
The text was updated successfully, but these errors were encountered: