From 3efd46ea754edea399c0bac92fc722987c14ace1 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Sat, 26 Sep 2026 05:23:05 -0300 Subject: [PATCH] Fix year check to use the display time zone instead of UTC The default year option compared UTC years, so a date that falls in a different calendar year in the element's time zone (or the browser's) lost its year, or got one it did not need, near New Year. --- src/relative-time-element.ts | 3 ++- test/relative-time.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/relative-time-element.ts b/src/relative-time-element.ts index 4ed38b14..2daf2d14 100644 --- a/src/relative-time-element.ts +++ b/src/relative-time-element.ts @@ -516,7 +516,8 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor const year = this.getAttribute('year') if (year === 'numeric' || year === '2-digit') return year - if (!this.hasAttribute('year') && new Date().getUTCFullYear() !== this.date?.getUTCFullYear()) { + const date = this.date + if (!this.hasAttribute('year') && !(date && this.#isCurrentYear(date, this.#lang, this.timeZone))) { return 'numeric' } } diff --git a/test/relative-time.js b/test/relative-time.js index 14fdd8c4..563a1344 100644 --- a/test/relative-time.js +++ b/test/relative-time.js @@ -3274,5 +3274,15 @@ suite('relative-time', function () { assert.equal(time.shadowRoot.textContent, 'Wed, Jan 1, 2020, 9:00:00 PM') document.documentElement.removeAttribute('time-zone') }) + + test('shows the year when the date is in a past year in the time-zone', async () => { + freezeTime(new Date('2023-06-15T12:00:00.000Z')) + const el = document.createElement('relative-time') + el.setAttribute('datetime', '2023-01-01T02:00:00.000Z') + el.setAttribute('time-zone', 'America/New_York') + el.setAttribute('format', 'datetime') + await Promise.resolve() + assert.equal(el.shadowRoot.textContent, 'Sat, Dec 31, 2022') + }) }) })