/** * Calculates the difference in months between two dates. * @param date1 - The first date. * @param date2 - The second date. */ function dateDiffInMonths(date1: number, date2: number): number { date1 = Number(date1); date2 = Number(date2); if (date1 <= 0 || date2 <= 0) { return 0; } // Extract year and month from both dates const year1 = new Date(date1).getFullYear(); const year2 = new Date(date2).getFullYear(); const month1 = new Date(date1).getMonth(); const month2 = new Date(date2).getMonth(); // Calculate the difference in months if (date1 > date2) { return month1 + 12 * year1 - (month2 + 12 * year2); } return month2 + 12 * year2 - (month1 + 12 * year1); } // Example usage: // console.log(dateDiffInMonths(date1, date2)); // Output: 43 export default dateDiffInMonths;