Skip to content

Commit 847ee5f

Browse files
committed
Fix NPE in time.js
1 parent f82adb6 commit 847ee5f

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

assets/javascript/time.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
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+
}

0 commit comments

Comments
 (0)