Open haoyuanqiang opened 4 years ago
The problem with this is that month durations vary according to which month/year it is. It could be any value between 28-31. moment
averages the amount of days in a month by a span of 400 years which turns out to be 30.436875
. As to why they are not consistent, days are being rounded down (from 30.436875
) otherwise those are the days you would get in 1 month.
I'm unsure if moment
can ever guarantee:
let n = months;
let days = moment.duration(n, 'months').asDays();
let nMonths = moment.duration(days, 'd').asMonths();
n === nMonths;
For any months
(even if days are not rounded). At the very least not without some library that can handle precision accurately or changing how moment
calculates days for each month.
Describe the bug I discovered one problem while using moment.duration to convert duration. The duration is more than one month.
The expected output is
1-25 4:42:13
, but the real output is1-24 4:42:13
. I thought one month was 30 days but it's not.I wrote one small program for test:
Why are the days inconsistent between two method constructing a duration of one month?
To Reproduce Run these code to reproduce the behavior:
Expected behavior
The days of one month are expected to an accurate number whether it is 30 or whatever
Screenshots none
Desktop (please complete the following information):
Moment-specific environment
Additional context none