@@ -32,6 +32,14 @@ function getUnitFactor(el: RelativeTimeElement): number {
3232 return 60 * 60 * 1000
3333}
3434
35+ // Determine whether the user has a 12 (vs. 24) hour cycle preference. This relies on the hour formatting in
36+ // a 12 hour preference being formatted like "1 AM" including a space, while with a 24 hour preference, the
37+ // same is formatted as "01" without a space. In the future `Intl.Locale.prototype.getHourCycles()` could be
38+ // used but in my testing it incorrectly returned a 12 hour preference with MacOS set to 24 hour format.
39+ function isBrowser12hCycle ( ) {
40+ return Boolean ( new Intl . DateTimeFormat ( [ ] , { hour : 'numeric' } ) . format ( 0 ) . match ( / \s / ) )
41+ }
42+
3543const dateObserver = new ( class {
3644 elements : Set < RelativeTimeElement > = new Set ( )
3745 time = Infinity
@@ -130,6 +138,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
130138 // value takes precedence over this custom format.
131139 //
132140 // Returns a formatted time String.
141+
133142 #getFormattedTitle( date : Date ) : string | undefined {
134143 return new Intl . DateTimeFormat ( this . #lang, {
135144 day : 'numeric' ,
@@ -139,6 +148,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
139148 minute : '2-digit' ,
140149 timeZoneName : 'short' ,
141150 timeZone : this . timeZone ,
151+ hour12 : isBrowser12hCycle ( ) ,
142152 } ) . format ( date )
143153 }
144154
@@ -213,6 +223,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
213223 year : this . year ,
214224 timeZoneName : this . timeZoneName ,
215225 timeZone : this . timeZone ,
226+ hour12 : isBrowser12hCycle ( ) ,
216227 } )
217228 return `${ this . prefix } ${ formatter . format ( date ) } ` . trim ( )
218229 }
@@ -246,6 +257,7 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
246257 minute : '2-digit' ,
247258 timeZoneName : 'short' ,
248259 timeZone : this . timeZone ,
260+ hour12 : isBrowser12hCycle ( ) ,
249261 }
250262
251263 if ( this . #isToday( date ) ) {
0 commit comments