How to get the number of months between two dates in Node.js

1 Answer

0 votes
function getMonthsBetween2Dates(date1, date2) {
    return date2.getMonth() - date1.getMonth() + 12 * (date2.getFullYear() - date1.getFullYear());
}


console.log(getMonthsBetween2Dates(new Date('2022-03-9'), new Date('2022-11-18')));

  
  
  
  
/*
run:
  
8
  
*/

 



answered Apr 9, 2022 by avibootz
...