From 67985719ebc7e93ca8082ddcc29edf8e1852e582 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Fri, 19 Sep 2025 15:19:29 -0400 Subject: [PATCH] Polyfill: Speed up largestUnit: month date difference for calendars without leap months Fixes #3153 --- polyfill/lib/calendar.mjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/polyfill/lib/calendar.mjs b/polyfill/lib/calendar.mjs index 60eca0ecf..031fb2311 100644 --- a/polyfill/lib/calendar.mjs +++ b/polyfill/lib/calendar.mjs @@ -1013,7 +1013,7 @@ const nonIsoHelperBase = { } const diffYears = calendarTwo.year - calendarOne.year; const diffDays = calendarTwo.day - calendarOne.day; - if (largestUnit === 'year' && diffYears) { + if (diffYears && (largestUnit === 'year' || !monthCodeInfo[this.id]?.additionalMonths)) { let diffInYearSign = 0; if (calendarTwo.monthCode > calendarOne.monthCode) diffInYearSign = 1; if (calendarTwo.monthCode < calendarOne.monthCode) diffInYearSign = -1; @@ -1022,6 +1022,10 @@ const nonIsoHelperBase = { years = isOneFurtherInYear ? diffYears - sign : diffYears; } const yearsAdded = years ? this.addCalendar(calendarOne, { years }, 'constrain', cache) : calendarOne; + if (years && largestUnit === 'month') { + months += years * 12; + years = 0; + } // Now we have less than one year remaining. Add one month at a time // until we go over the target, then back up one month and calculate // remaining days and weeks.