1
1
export function toAbsoluteLocaleDate ( date : string , lang : string , opts : Intl . DateTimeFormatOptions ) {
2
- return new Date ( date ) . toLocaleString ( lang || [ ] , opts ) ;
2
+ // only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) or (YYYY-MM-DD)
3
+ // if there is an "Invalid Date" error, there must be something wrong in code and should be fixed.
4
+ const dateSep = date . indexOf ( 'T' ) ;
5
+ date = dateSep === - 1 ? date : date . substring ( 0 , dateSep ) ;
6
+ return new Date ( `${ date } T00:00:00` ) . toLocaleString ( lang || [ ] , opts ) ;
3
7
}
4
8
5
9
window . customElements . define ( 'absolute-date' , class extends HTMLElement {
@@ -15,14 +19,8 @@ window.customElements.define('absolute-date', class extends HTMLElement {
15
19
const lang = this . closest ( '[lang]' ) ?. getAttribute ( 'lang' ) ||
16
20
this . ownerDocument . documentElement . getAttribute ( 'lang' ) || '' ;
17
21
18
- // only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
19
- let date = this . getAttribute ( 'date' ) ;
20
- let dateSep = date . indexOf ( 'T' ) ;
21
- dateSep = dateSep === - 1 ? date . indexOf ( ' ' ) : dateSep ;
22
- date = dateSep === - 1 ? date : date . substring ( 0 , dateSep ) ;
23
-
24
22
if ( ! this . shadowRoot ) this . attachShadow ( { mode : 'open' } ) ;
25
- this . shadowRoot . textContent = toAbsoluteLocaleDate ( date , lang , opt ) ;
23
+ this . shadowRoot . textContent = toAbsoluteLocaleDate ( this . getAttribute ( ' date' ) , lang , opt ) ;
26
24
} ;
27
25
28
26
attributeChangedCallback ( _name , oldValue , newValue ) {
0 commit comments