### new Date() - new Date() 是用來創建表示當前日期和時間的物件的語法。這個物件包含了年、月、日、時、分、秒等相關的資訊,並提供了多種方法來操作日期和時間。 如果你呼叫 new Date() 而不傳入任何參數,它將返回當前的日期和時間。例如: ```js const currentDate = new Date(); console.log('現在時間是:',currentDate) // output: Thu Dec 07 2023 21:01:20 GMT+0800 (台北標準時間) ``` --- ### [.getTime()](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) - 是一個搭配 new Date() 方法的 method - `.getTime()` 方法通常搭配 `new Date()` 使用,因為它是 Date 物件的一個方法,用來取得該日期物件對應的時間戳(時間表示的毫秒數)。 當你需要處理日期和時間時,你可以使用 new Date() 創建一個日期物件,然後使用 .getTime() 方法獲得該日期對應的時間戳。這對於計算兩個日期之間的時間差、進行日期比較或者在需要時間戳的情境下非常有用。 ```js let currentTime = new Date(); console.log(currentTime.getTime()); // outputs 1701954107852 ```
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.