date-manip
Version:
A lightweight JavaScript date utility library that provides modularity, high performance, and additional features. It supports various date operations, including date addition and subtraction, formatting, comparison, etc.
23 lines (22 loc) • 716 B
JavaScript
;
const addMonths = require("./addMonths.js");
function monthDiff(a, b) {
const aTime = +a;
const bTime = +b;
if (aTime < bTime) {
return -monthDiff(b, a);
}
const wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth());
const anchor = +addMonths(new Date(aTime), wholeMonthDiff);
let anchor2;
let adjust;
if (bTime < anchor) {
anchor2 = +addMonths(new Date(aTime), wholeMonthDiff - 1);
adjust = (bTime - anchor) / (anchor - anchor2);
} else {
anchor2 = +addMonths(new Date(aTime), wholeMonthDiff + 1);
adjust = (bTime - anchor) / (anchor2 - anchor);
}
return -(wholeMonthDiff + adjust) || 0;
}
exports.monthDiff = monthDiff;