Skip to content

Commit ad9c6ff

Browse files
gibson042ptomato
authored andcommitted
Polyfill: Speed up largestUnit: month date difference for calendars without leap months
Fixes #3153
1 parent 6274e5d commit ad9c6ff

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

polyfill/lib/calendar.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ const nonIsoHelperBase = {
10131013
}
10141014
const diffYears = calendarTwo.year - calendarOne.year;
10151015
const diffDays = calendarTwo.day - calendarOne.day;
1016-
if (largestUnit === 'year' && diffYears) {
1016+
if (diffYears && (largestUnit === 'year' || !monthCodeInfo[this.id]?.additionalMonths)) {
10171017
let diffInYearSign = 0;
10181018
if (calendarTwo.monthCode > calendarOne.monthCode) diffInYearSign = 1;
10191019
if (calendarTwo.monthCode < calendarOne.monthCode) diffInYearSign = -1;
@@ -1022,6 +1022,10 @@ const nonIsoHelperBase = {
10221022
years = isOneFurtherInYear ? diffYears - sign : diffYears;
10231023
}
10241024
const yearsAdded = years ? this.addCalendar(calendarOne, { years }, 'constrain', cache) : calendarOne;
1025+
if (years && largestUnit === 'month') {
1026+
months += years * 12;
1027+
years = 0;
1028+
}
10251029
// Now we have less than one year remaining. Add one month at a time
10261030
// until we go over the target, then back up one month and calculate
10271031
// remaining days and weeks.

0 commit comments

Comments
 (0)