-
Notifications
You must be signed in to change notification settings - Fork 0
/
js004.js
49 lines (45 loc) · 1.28 KB
/
js004.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function getTime() {
let location = document.querySelectorAll('.location')
let dateText = document.querySelectorAll('.date')
let timeText = document.querySelectorAll('.time')
let timezone = {
'NEW YORK': 'America/New_York',
LONDON: 'Europe/London',
BANGKOK: 'Asia/Bangkok',
TAIWAN: 'Asia/Taipei',
SYDNEY: 'Australia/Sydney',
}
location.forEach((item, index) => {
timeText[index].textContent = currentTime(
timezone[item.textContent]
).time
dateText[index].textContent = `${
currentTime(timezone[item.textContent]).date
} ${currentTime(timezone[item.textContent]).year}`
})
}
function currentTime(timeZone) {
let locale = 'en-US'
let options_YYYY = {
timeZone,
year: 'numeric',
}
let options_HHMM = {
timeZone,
hour12: false,
hour: 'numeric',
minute: '2-digit',
}
let options_MMDD = {
timeZone,
month: 'short',
day: 'numeric',
}
return {
year: new Date().toLocaleString(locale, options_YYYY),
date: new Date().toLocaleString(locale, options_MMDD),
time: new Date().toLocaleString(locale, options_HHMM),
}
}
getTime()
window.setInterval(getTime, 1000)