blob: 4e7251138b1fdce00d09322ad4b710f34ef842ca (
plain) (
blame)
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
|
function settime() {
const timestamp = document.querySelector('[data-timestamp-text]')
if (!timestamp || !('Intl' in window)) return
const options = {
timeZone: "Asia/Taipei",
timeStyle: "short",
hour12: false
}
// https://gist.github.com/muan/e7414b6241f088090acd916ed965540e
let time = new Intl.DateTimeFormat(navigator.language || "zh-TW", options).format(new Date())
// https://bugs.chromium.org/p/chromium/issues/detail?id=1262801
if (time.match(/^24:/)) time = time.replace('24:', '00:')
// Setting interpolated string instead of just the time because
// if there's no JS there should be no mentions of current time
const text = timestamp.getAttribute('data-timestamp-text').replace('{time}', time)
timestamp.innerHTML = text.replace(':', '<span class="timestamp-colon" data-colon>:</span>')
const now = new Date()
const sec = now.getSeconds()
const secondIsEven = sec % 2 === 0
const colon = document.querySelector('[data-colon]')
if (colon) colon.style.animationDelay = `${(secondIsEven ? 0 : 1000) - now.getMilliseconds()}ms`
const delay = 60000 - ((sec * 1000) + now.getMilliseconds())
setTimeout(settime, delay)
}
settime()
|