File tree Expand file tree Collapse file tree 1 file changed +21
-17
lines changed
Expand file tree Collapse file tree 1 file changed +21
-17
lines changed Original file line number Diff line number Diff line change 1- // Option 1: Get from a data attribute
2- const utcDateString = document . getElementById ( 'convertedTime' ) . dataset . utc ;
1+ // Option 1: Get from a data attribute
2+ const convertedTimeElement = document . getElementById ( 'convertedTime' ) ;
33
4- // Convert to user's local date and time
5- const localDate = new Date ( utcDateString ) ;
4+ if ( convertedTimeElement != null ) {
5+ const utcDateString = convertedTimeElement . dataset . utc ;
66
7- // Format the output
8- const options = {
9- weekday : 'long' ,
10- year : 'numeric' ,
11- month : 'long' ,
12- day : 'numeric' ,
13- hour : '2-digit' ,
14- minute : '2-digit' ,
15- timeZoneName : 'short'
16- } ;
7+ // Convert to user's local date and time
8+ const localDate = new Date ( utcDateString ) ;
179
18- const localDateTimeString = localDate . toLocaleString ( undefined , options ) ;
10+ // Format the output
11+ const options = {
12+ weekday : 'long' ,
13+ year : 'numeric' ,
14+ month : 'long' ,
15+ day : 'numeric' ,
16+ hour : '2-digit' ,
17+ minute : '2-digit' ,
18+ timeZoneName : 'short'
19+ } ;
1920
20- // Set the readable local date as content
21- document . getElementById ( "convertedTime" ) . textContent = `Date & Time: ${ localDateTimeString } ` ;
21+ const localDateTimeString = localDate . toLocaleString ( undefined , options ) ;
22+
23+ // Set the readable local date as content
24+ convertedTimeElement . textContent = `Date & Time: ${ localDateTimeString } ` ;
25+ }
You can’t perform that action at this time.
0 commit comments