-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Inconsistency with +moment.duration() and moment.duration().asMilliseconds() #5603
Comments
I checked the source code, and I think the calculation based on 61 days is more accurate, what do you think @rw103 @marwahaha? I am happy to submit a pull request 😄 . Source Code
This // TODO: Use this.as('ms')?
export function valueOf() {
if (!this.isValid()) {
return NaN;
}
return (
this._milliseconds +
this._days * 864e5 +
(this._months % 12) * 2592e6 +
toInt(this._months / 12) * 31536e6
);
}
This function as(units) {
// ...
// handle milliseconds separately because of floating point math errors (issue #1867)
days = this._days + Math.round(monthsToDays(this._months));
// ...
} function monthsToDays(months) {
// the reverse of daysToMonths
return (months * 146097) / 4800;
} |
I think this could be a breaking change out there in the wild. It does kind of make sense for both functions to return the same amount of milliseconds though. |
Yeah, I originally said 60 days makes more sense for expected behavior because I didn't know the rationale for 61 days. But it's true that not every month is 30 days, so using an average month duration makes sense. The more important thing is to make both functions consistent. |
I have a similar problem.
Unfortunately maxTimeRange is less than duration |
I have a similar issue where I convert 90 months into milliseconds and then back into months. This offset is caused by the rounding:
I do not understand why the 365.25/12 is not simply used as a conversion factor. |
Describe the bug
+moment.duration({ months: 2 })
returns5184000000
(60 days) butmoment.duration({ months: 2 }).asMilliseconds()
returns5270400000
(61 days)To Reproduce
Run the following code in Node.
Expected behavior
I expect the following output (60 days in ms for both):
Instead, I get this output:
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: