-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Calculation of diff in years is wrong for leap-years (since v1.8.32) #1000
Comments
Yes, we made an update 19e8a7f, to keep the diff result the same with the current version of moment.js (2.27.0). And this is also a bug on moment side. We will try to find some workaround to fix this issue. |
This is an interesting issue. I just met the same problem, and someone told me that
|
const d = require("dayjs");
const duration = require('dayjs/plugin/duration');
const isBetween = require('dayjs/plugin/isBetween');
const isLeapYear = require('dayjs/plugin/isLeapYear');
d.extend(duration);
d.extend(isBetween);
d.extend(isLeapYear);
const dateDiff = (startStr, endStr = new Date()) => {
let startDate = d(startStr);
let endDate = d(endStr);
let diff = endDate.diff(startDate, 'days');
for (let year = startDate.year(); year <= endDate.year(); year++) {
const date = d(year + '-02-29');
if (date.isBetween(startDate,endDate) && date.isLeapYear()) {
diff -= 1;
}
}
return diff;
}
const durationValue = d.duration(dateDiff('1990-08-16'), "days");
console.log(`${durationValue.years()} - ${durationValue.months()} - ${durationValue.days()}`)
Here is what I did |
Hi! Any news as to when will this be fixed? having the same issue. |
We use
diff()
to calculate the current age of people according to their date of birth. For someone who is born on February 29th at a leap-year, this calculation seems to be broken since version 1.8.32Taken a person born on 2016-02-29, with version 1.8.31 on February 28th 2017 this person is still 0 years old. On March 1st, this person turns 1.
With version 1.8.32, this person is already 1 year old on February 28th 2017, which is - at least here in Germany - not correct.
If the calculation is carried out in a leap year (e.g. 2020), it is correct in both versions.
Fiddle for version 1.8.31: https://jsfiddle.net/enqbx39r/1
Fiddle for version 1.8.32: https://jsfiddle.net/49yfb7wt/1
The text was updated successfully, but these errors were encountered: