How to get the number of months between two dates in TypeScript

1 Answer

0 votes
function getMonthsBetween2Dates(date1 : Date, date2 : Date) : number {
    return date2.getMonth() - date1.getMonth() + 12 * (date2.getFullYear() - date1.getFullYear());
}
 
 
console.log(getMonthsBetween2Dates(new Date('2022-01-7'), new Date('2022-8-17')));
 
   
   
   
   
/*
run:
   
7
   
*/

 



answered Apr 9, 2022 by avibootz
edited Apr 9, 2022 by avibootz
...