mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-04-11 00:56:16 +00:00
Fix age calc, missing leap day for years divisible by 100 and not by 400 (#96)
This commit is contained in:
@@ -32,6 +32,18 @@ describe('calcAge()', () => {
|
|||||||
const age = calcAge('2 Sep 1990', '1 Sep 2021', intl);
|
const age = calcAge('2 Sep 1990', '1 Sep 2021', intl);
|
||||||
expect(age).toEqual('30 years');
|
expect(age).toEqual('30 years');
|
||||||
});
|
});
|
||||||
|
it('age respecting missing leap year divisible by 100 and not divisible by 400', () => {
|
||||||
|
const age = calcAge('1890', '1921', intl);
|
||||||
|
expect(age).toEqual('31 years');
|
||||||
|
});
|
||||||
|
it('age respecting missing leap year divisible by 100 and not divisible by 400 for full dates', () => {
|
||||||
|
const age = calcAge('1 Sep 1890', '1 Sep 1921', intl);
|
||||||
|
expect(age).toEqual('31 years');
|
||||||
|
});
|
||||||
|
it('age with round down respecting missing leap year divisible by 100 and not divisible by 400', () => {
|
||||||
|
const age = calcAge('2 Sep 1890', '1 Sep 1921', intl);
|
||||||
|
expect(age).toEqual('30 years');
|
||||||
|
});
|
||||||
|
|
||||||
it('age with exact and range between', () => {
|
it('age with exact and range between', () => {
|
||||||
const age = calcAge('1990', 'BET 2020 AND 2021', intl);
|
const age = calcAge('1990', 'BET 2020 AND 2021', intl);
|
||||||
|
|||||||
@@ -115,10 +115,21 @@ function calcDateDifferenceInYears(
|
|||||||
const firstDateObject = toDateObject(firstDate);
|
const firstDateObject = toDateObject(firstDate);
|
||||||
const secondDateObject = toDateObject(secondDate);
|
const secondDateObject = toDateObject(secondDate);
|
||||||
|
|
||||||
const dateDiff = new Date(
|
const startYear = firstDateObject.getUTCFullYear();
|
||||||
secondDateObject.valueOf() - firstDateObject.valueOf(),
|
|
||||||
);
|
let yearDiff = secondDateObject.getUTCFullYear() - startYear;
|
||||||
return Math.abs(dateDiff.getUTCFullYear() - 1970);
|
let monthDiff = secondDateObject.getUTCMonth() - firstDateObject.getUTCMonth();
|
||||||
|
if (monthDiff < 0) {
|
||||||
|
yearDiff--;
|
||||||
|
monthDiff += 12;
|
||||||
|
}
|
||||||
|
const dayDiff = secondDateObject.getUTCDate() - firstDateObject.getUTCDate();
|
||||||
|
if (dayDiff < 0) {
|
||||||
|
if (monthDiff <= 0) {
|
||||||
|
yearDiff--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Math.abs(yearDiff);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function calcAge(
|
export function calcAge(
|
||||||
|
|||||||
Reference in New Issue
Block a user