date-differencer

Calculate the time interval between two Date
objects and output the result in years plus months plus days plus hours plus minutes plus seconds plus milliseconds (instead of representing the same duration in different units). This library is useful for lifespan check and age calculation.
Usage
import {
dateDiff, dateTimeDiff, dayDiff, dayTimeDiff,
addDateTimeDiff, addDayTimeDiff
} from "date-differencer";
const a = new Date(2022, 5, 6, 0);
const b = new Date(2023, 7, 9, 1);
console.log(dateDiff(a, b));
console.log(dateTimeDiff(a, b));
console.log(Math.trunc(dayDiff(a, b)));
console.log(dayTimeDiff(a, b));
console.log(addDateTimeDiff(a, dateTimeDiff(a, b)));
console.log(addDayTimeDiff(a, dayTimeDiff(a, b)));
This library can handle leap years and odd/even number of days in a month correctly. The result of following code is a bit confusing but reasonable.
import { dateDiff } from "date-differencer";
const a = new Date("2020-02-27");
const b = new Date("2021-03-01");
console.log(dateDiff(a, b));
console.log(dateDiff(b, a));
Usage for Browsers
Source
Demo Page
License
MIT