# new Date() [MDN Web Docs](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date) > Date 物件是基於世界標準時間(UTC) 1970 年 1 月 1 日開始的毫秒數值來儲存時間。 ## 語法 ```javascript= new Date() ``` ### 獲取其他精細的時間 [Date.prototype 方法](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date#date.prototype_%E6%96%B9%E6%B3%95) ```javascript= Date.prototype.getHours() // 回傳本地時間的小時(0-23) Date.prototype.getSeconds() // 回傳本地時間的秒數(0-59) ``` ## 範例:利用 setInterval 跟時間結合,印出秒數 ```javascript= function setDate() { const now = new Date() const second = now.getSeconds() console.log(second) } setInterval(setDate, 1000) ``` ## 範例:利用得到的秒數,結合時鐘秒針轉換成角度方式  html ```htmlembedded= <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 ```javascript= 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) ``` ###### tags: `JS` {%hackmd @unayojanni/H1Qq0uKkK %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up