Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 569 Bytes

getMonthsDiffBetweenDates.md

File metadata and controls

22 lines (18 loc) · 569 Bytes
title tags
getMonthsDiffBetweenDates
date,intermediate

Returns the difference (in months) between two dates.

  • Use Date.prototype.getFullYear() and Date.prototype.getMonth() to calculate the difference (in months) between two Date objects.
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