diff --git a/polyfill/lib/date.mjs b/polyfill/lib/date.mjs index b0a36c3319..74074a2877 100644 --- a/polyfill/lib/date.mjs +++ b/polyfill/lib/date.mjs @@ -125,12 +125,13 @@ export class Date { difference(other, options) { if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver'); if (!ES.IsTemporalDate(other)) throw new TypeError('invalid Date object'); - const largestUnit = ES.ToLargestTemporalUnit(options, 'days', ['hours', 'minutes', 'seconds']); + const calendar = GetSlot(this, CALENDAR); + if (calendar.id !== GetSlot(other, CALENDAR).id) { + other = new Date(GetSlot(other, ISO_YEAR), GetSlot(other, ISO_MONTH), GetSlot(other, ISO_DAY), calendar); + } const comparison = Date.compare(this, other); if (comparison < 0) throw new RangeError('other instance cannot be larger than `this`'); - const { years, months, weeks, days } = ES.DifferenceDate(other, this, largestUnit); - const Duration = GetIntrinsic('%Temporal.Duration%'); - return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); + return calendar.difference(other, this, options); } equals(other) { if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver'); diff --git a/polyfill/lib/datetime.mjs b/polyfill/lib/datetime.mjs index fedc206fde..4937485492 100644 --- a/polyfill/lib/datetime.mjs +++ b/polyfill/lib/datetime.mjs @@ -245,8 +245,9 @@ export class DateTime { other, this ); - let { year, month, day } = this; - day += deltaDays; + let year = GetSlot(larger, ISO_YEAR); + let month = GetSlot(larger, ISO_MONTH); + let day = GetSlot(larger, ISO_DAY) + deltaDays; ({ year, month, day } = ES.BalanceDate(year, month, day)); let dateLargestUnit = 'days'; @@ -254,8 +255,7 @@ export class DateTime { dateLargestUnit = largestUnit; } - let { years, months, weeks, days } = ES.DifferenceDate(other, { year, month, day }, dateLargestUnit); - + let days; ({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceDuration( days, hours, diff --git a/polyfill/lib/yearmonth.mjs b/polyfill/lib/yearmonth.mjs index adee358fe5..674d754b07 100644 --- a/polyfill/lib/yearmonth.mjs +++ b/polyfill/lib/yearmonth.mjs @@ -102,18 +102,13 @@ export class YearMonth { const largestUnit = ES.ToLargestTemporalUnit(options, 'years', ['weeks', 'days', 'hours', 'minutes', 'seconds']); const comparison = YearMonth.compare(this, other); if (comparison < 0) throw new RangeError('other instance cannot be larger than `this`'); - let years = this.year - other.year; - let months = this.month - other.month; - if (months < 0) { - years -= 1; - months += 12; - } - if (largestUnit === 'months') { - months += 12 * years; - years = 0; - } - const Duration = GetIntrinsic('%Temporal.Duration%'); - return new Duration(years, months); + + const smallerFields = ES.ToTemporalYearMonthRecord(other); + const largerFields = ES.ToTemporalYearMonthRecord(this); + const TemporalDate = GetIntrinsic('%Temporal.Date%'); + const smaller = calendar.dateFromFields({ ...smallerFields, day: 1 }, {}, TemporalDate); + const larger = calendar.dateFromFields({ ...largerFields, day: 1 }, {}, TemporalDate); + return calendar.difference(smaller, larger, { ...options, largestUnit }); } equals(other) { if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');