1
+ import { Temporal } from 'temporal-polyfill' ;
2
+
3
+ export function toAbsoluteLocaleDate ( dateStr , lang , opts ) {
4
+ return Temporal . PlainDate . from ( dateStr ) . toLocaleString ( lang ?? [ ] , opts ) ;
5
+ }
6
+
1
7
window . customElements . define ( 'gitea-absolute-date' , class extends HTMLElement {
2
8
static observedAttributes = [ 'date' , 'year' , 'month' , 'weekday' , 'day' ] ;
3
9
@@ -7,19 +13,13 @@ window.customElements.define('gitea-absolute-date', class extends HTMLElement {
7
13
const weekday = this . getAttribute ( 'weekday' ) ?? '' ;
8
14
const day = this . getAttribute ( 'day' ) ?? '' ;
9
15
const lang = this . closest ( '[lang]' ) ?. getAttribute ( 'lang' ) ||
10
- this . ownerDocument . documentElement . getAttribute ( 'lang' ) ||
11
- '' ;
16
+ this . ownerDocument . documentElement . getAttribute ( 'lang' ) || '' ;
12
17
13
- // only extract the `yyyy-mm-dd` part. When converting to Date, it will become midnight UTC and when rendered
14
- // as localized date, will have a offset towards UTC, which we remove to shift the timestamp to midnight in the
15
- // localized date. We should eventually use `Temporal.PlainDate` which will make the correction unnecessary.
16
- // - https://stackoverflow.com/a/14569783/808699
17
- // - https://tc39.es/proposal-temporal/docs/plaindate.html
18
- const date = new Date ( this . getAttribute ( 'date' ) . substring ( 0 , 10 ) ) ;
19
- const correctedDate = new Date ( date . getTime ( ) - date . getTimezoneOffset ( ) * - 60000 ) ;
18
+ // only use the first 10 characters, e.g. the `yyyy-mm-dd` part
19
+ const dateStr = this . getAttribute ( 'date' ) . substring ( 0 , 10 ) ;
20
20
21
21
if ( ! this . shadowRoot ) this . attachShadow ( { mode : 'open' } ) ;
22
- this . shadowRoot . textContent = correctedDate . toLocaleString ( lang ?? [ ] , {
22
+ this . shadowRoot . textContent = toAbsoluteLocaleDate ( dateStr , lang , {
23
23
...( year && { year} ) ,
24
24
...( month && { month} ) ,
25
25
...( weekday && { weekday} ) ,
0 commit comments