13
13
14
14
// The class for the custom element
15
15
export class DateTimeElement extends HTMLElement {
16
- public shadowRoot : ShadowRoot ;
17
- private dateElement : HTMLElement ;
18
- private timeElement : HTMLElement ;
19
- private culture : string = 'en-US' ;
20
- private dateFormat : string = '' ;
21
- private timeFormat : string = '' ;
16
+ public shadowRoot : ShadowRoot
17
+ private dateElement : HTMLElement
18
+ private timeElement : HTMLElement
19
+ private culture = 'en-US'
20
+ private dateFormat = ''
21
+ private timeFormat = ''
22
22
// Observe the format attribute for changes
23
23
static get observedAttributes ( ) : string [ ] {
24
- return [ 'date-format' , 'time-format' , 'culture' ] ;
24
+ return [ 'date-format' , 'time-format' , 'culture' ]
25
25
}
26
26
constructor ( ) {
27
- super ( ) ;
27
+ super ( )
28
28
29
29
// Attach a shadow root to the custom element
30
- this . shadowRoot = this . attachShadow ( { mode : 'open' } ) ;
30
+ this . shadowRoot = this . attachShadow ( { mode : 'open' } )
31
31
32
32
// Add the template to the shadow root
33
- const template = document . createElement ( 'template' ) ;
33
+ const template = document . createElement ( 'template' )
34
+
34
35
template . innerHTML = `
35
36
<style>
36
37
#time-container {
@@ -48,79 +49,78 @@ export class DateTimeElement extends HTMLElement {
48
49
<p id="date"></p>
49
50
<p id="time"></p>
50
51
</div>
51
- ` ;
52
- this . shadowRoot . appendChild ( template . content . cloneNode ( true ) ) ;
52
+ `
53
+ this . shadowRoot . appendChild ( template . content . cloneNode ( true ) )
53
54
54
55
// Get the date and time elements
55
- this . dateElement = this . shadowRoot . querySelector ( '#date' ) ! ;
56
- this . timeElement = this . shadowRoot . querySelector ( '#time' ) ! ;
56
+ this . dateElement = this . shadowRoot . querySelector ( '#date' ) !
57
+ this . timeElement = this . shadowRoot . querySelector ( '#time' ) !
57
58
}
58
59
59
60
// Update the element when the format attribute changes
60
61
attributeChangedCallback ( name : string , oldValue : string , newValue : string ) : void {
61
62
if ( ( name === 'date-format' || name === 'time-format' ) && newValue !== oldValue ) {
62
63
// Get the current values of the date-format and time-format attributes
63
- this . dateFormat = this . getAttribute ( 'date-format' ) || '' ;
64
- this . timeFormat = this . getAttribute ( 'time-format' ) || '' ;
65
- this . culture = this . getAttribute ( 'culture' ) || 'en-US' ;
64
+ this . dateFormat = this . getAttribute ( 'date-format' ) || ''
65
+ this . timeFormat = this . getAttribute ( 'time-format' ) || ''
66
+ this . culture = this . getAttribute ( 'culture' ) || 'en-US'
66
67
// Update the formatted date and time values using the new formats
67
- this . updateFormattedDate ( this . dateFormat ) ;
68
- this . updateFormattedTime ( this . timeFormat ) ;
68
+ this . updateFormattedDate ( this . dateFormat )
69
+ this . updateFormattedTime ( this . timeFormat )
69
70
70
71
// Update the element every second
71
72
setInterval ( ( ) => {
72
- this . updateFormattedDate ( this . dateFormat ) ;
73
- this . updateFormattedTime ( this . timeFormat ) ;
74
- } , 1000 ) ;
73
+ this . updateFormattedDate ( this . dateFormat )
74
+ this . updateFormattedTime ( this . timeFormat )
75
+ } , 1000 )
75
76
}
76
77
}
77
78
78
79
private updateFormattedTime ( format : string ) : void {
79
80
// Get the current time
80
- const now = new Date ( ) ;
81
+ const now = new Date ( )
81
82
82
83
// Use the Intl.DateTimeFormat API to format the time
83
84
const timeOptions = {
84
85
hour : format . includes ( 'h' ) ? 'numeric' : undefined ,
85
86
minute : format . includes ( 'm' ) ? 'numeric' : undefined ,
86
87
second : format . includes ( 's' ) ? 'numeric' : undefined ,
87
- hour12 : format . includes ( '12' ) ? undefined : false
88
- } as const ;
89
- const formattedTime = new Intl . DateTimeFormat ( this . culture , timeOptions ) . format ( now ) ;
88
+ hour12 : format . includes ( '12' ) ? undefined : false ,
89
+ } as const
90
+ const formattedTime = new Intl . DateTimeFormat ( this . culture , timeOptions ) . format ( now )
90
91
91
92
// Update the formatted time element with the formatted time
92
- this . timeElement . textContent = formattedTime ;
93
+ this . timeElement . textContent = formattedTime
93
94
}
94
95
95
-
96
96
private updateFormattedDate ( format : string ) : void {
97
97
// Get the current date
98
- const now = new Date ( ) ;
98
+ const now = new Date ( )
99
99
100
100
// Use the Intl.DateTimeFormat API to format the date
101
101
const dateOptions = {
102
102
year : format . includes ( 'y' ) ? 'numeric' : undefined ,
103
103
month : format . includes ( 'm' ) ? 'numeric' : undefined ,
104
104
day : format . includes ( 'd' ) ? 'numeric' : undefined ,
105
- } as const ;
106
- const formattedDate = new Intl . DateTimeFormat ( this . culture , dateOptions ) . format ( now ) ;
105
+ } as const
106
+ const formattedDate = new Intl . DateTimeFormat ( this . culture , dateOptions ) . format ( now )
107
107
108
108
// Update the formatted date element with the formatted date
109
- this . dateElement . textContent = formattedDate ;
109
+ this . dateElement . textContent = formattedDate
110
110
}
111
111
112
112
// Lifecycle callback that is called when the element is inserted into the DOM
113
113
connectedCallback ( ) {
114
114
if ( this . dateFormat === '' && this . timeFormat === '' ) {
115
- this . updateTime ( ) ;
116
- setInterval ( ( ) => this . updateTime ( ) , 1000 ) ;
115
+ this . updateTime ( )
116
+ setInterval ( ( ) => this . updateTime ( ) , 1000 )
117
117
}
118
118
}
119
119
120
120
updateTime ( ) {
121
- const now = new Date ( ) ;
122
- this . dateElement . textContent = now . toDateString ( ) ;
123
- this . timeElement . textContent = now . toTimeString ( ) ;
121
+ const now = new Date ( )
122
+ this . dateElement . textContent = now . toDateString ( )
123
+ this . timeElement . textContent = now . toTimeString ( )
124
124
}
125
125
}
126
126
declare global {
@@ -136,4 +136,4 @@ export default DateTimeElement
136
136
if ( ! window . customElements . get ( 'date-time' ) ) {
137
137
window . DateTimeElement = DateTimeElement
138
138
window . customElements . define ( 'date-time' , DateTimeElement )
139
- }
139
+ }
0 commit comments