Skip to content

Calculate the number of months between two dates in JavaScript

Advertisements
const monthsBtwnDates = (startDate, endDate) => {
startDate = new Date(startDate);
endDate = new Date(endDate);
  return Math.max(
    (endDate.getFullYear() - startDate.getFullYear()) * 12 +
      endDate.getMonth() -
      startDate.getMonth(),
    0
    
  )};
  
  
monthsBtwnDates('2019-12-21','2020-12-21')

//12

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.