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