Date 物件是基於世界標準時間(UTC) 1970 年 1 月 1 日開始的毫秒數值來儲存時間。
new Date()
Date.prototype.getHours()
// 回傳本地時間的小時(0-23)
Date.prototype.getSeconds()
// 回傳本地時間的秒數(0-59)
function setDate() {
const now = new Date()
const second = now.getSeconds()
console.log(second)
}
setInterval(setDate, 1000)
html
<div class="clock">
<div class="clock-liner center">
<div class="clock-dot center"></div>
<div class="hour-hand"></div>
<div class="min-hand"></div>
<div class="second-hand"></div>
</div>
</div>
js
const secondTime = document.querySelector('.second-hand')
function setDate() {
const now = new Date()
const second = now.getSeconds()
const secondDegree = ((second/60)*360 -90)
// 計算((秒數 / 六十格)*360度)
// 得到應該要的角度
// 但這邊的秒針因為定位起始要在12點鐘方向時,
// 為 270 度,所以要 -90 才會將秒針放置在12 點鐘方向
secondTime.style.transform = `rotate(${secondDegree}deg)`
console.log('second', second)
}
setInterval(setDate, 1000)
JS