Date difference in months

JavaScript, Date · Oct 19, 2020

Calculates the difference (in months) between two dates.

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

More like this