Skip to content
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

Open
amjmhs opened this issue Aug 12, 2020 · 5 comments
Open

Calculation of diff in years is wrong for leap-years (since v1.8.32) #1000

amjmhs opened this issue Aug 12, 2020 · 5 comments
Labels
☢️Bug Something isn't working

Comments

@amjmhs
Copy link

amjmhs commented Aug 12, 2020

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.32

Taken 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

@iamkun
Copy link
Owner

iamkun commented Aug 13, 2020

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.

@zazzaz
Copy link
Contributor

zazzaz commented Aug 23, 2020

This is an interesting issue.

I just met the same problem, and someone told me that

seems whether their birthday legally falls on Feb 28th or Mar 1st is dependent on the location
https://en.wikipedia.org/wiki/February_29#Legal_status
I'm not sure the question "Is one year after Feb 29th Mar 1st or Feb 28th?" has an absolutely correct answer.

@programmer-yang
Copy link

I had the same problem

image

@programmer-yang
Copy link

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

@ciprian-pratia-db
Copy link

Hi! Any news as to when will this be fixed? having the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants