660 B
660 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Date difference in months | snippet | javascript |
|
two-flower-vases | 2020-10-19T22:49:51+03:00 |
Calculates the difference (in months) between two dates.
- Use
Date.prototype.getFullYear()andDate.prototype.getMonth()to calculate the difference (in months) between twoDateobjects.
const getMonthsDiffBetweenDates = (dateInitial, dateFinal) =>
Math.max(
(dateFinal.getFullYear() - dateInitial.getFullYear()) * 12 +
dateFinal.getMonth() -
dateInitial.getMonth(),
0
);
getMonthsDiffBetweenDates(new Date('2017-12-13'), new Date('2018-04-29')); // 4