JavaScript Get Date Methods
The new Date() Constructor
在 JavaScript 中,日期物件使用 new Date()
建立。
new Date()
返回一個包含當前日期和時間的日期物件。
Date Get Methods
方法 | 描述 |
---|---|
getFullYear() | 獲取 年份(四位數,yyyy) |
getMonth() | 獲取 月份(數字,0-11) |
getDate() | 獲取 日期(數字,1-31) |
getDay() | 獲取 星期幾(數字,0-6) |
getHours() | 獲取 小時(0-23) |
getMinutes() | 獲取 分鐘(0-59) |
getSeconds() | 獲取 秒(0-59) |
getMilliseconds() | 獲取 毫秒(0-999) |
getTime() | 獲取 時間(自 1970 年 1 月 1 日起的毫秒數) |
注意 1
上面的 get 方法返回的是本地時間。
世界時(UTC)在本頁底部有說明。
注意 2
get 方法從現有的日期物件中獲取資訊。
日期物件中的時間是靜態的。它不是一個“執行”的“時鐘”。
日期物件中的時間與當前時間不相同。
getFullYear() 方法
getFullYear()
方法以四位數字返回日期的年份。
示例
const d = new Date("2021-03-25");
d.getFullYear();
自己動手試一試 »
const d = new Date();
d.getFullYear();
自己動手試一試 »
警告!
舊的 JavaScript 程式碼可能使用非標準方法 getYear()。
getYear() 應該返回一個兩位數的年份。
getYear() 已被棄用。請勿使用它!
getMonth() 方法
getMonth()
方法以數字(0-11)形式返回日期的月份。
注意
在 JavaScript 中,一月是月份編號 0,二月是 1,以此類推。
最後,十二月是月份編號 11。
示例
const d = new Date("2021-03-25");
d.getMonth();
自己動手試一試 »
const d = new Date();
d.getMonth();
自己動手試一試 »
注意
可以使用名稱陣列來返回月份的名稱。
示例
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
自己動手試一試 »
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const d = new Date();
let month = months[d.getMonth()];
自己動手試一試 »
getDate() 方法
getDate()
方法以數字(1-31)形式返回日期的日期。
示例
const d = new Date("2021-03-25");
d.getDate();
自己動手試一試 »
const d = new Date();
d.getDate();
自己動手試一試 »
getHours() 方法
getHours()
方法以數字(0-23)形式返回日期的小時。
示例
const d = new Date("2021-03-25");
d.getHours();
自己動手試一試 »
const d = new Date();
d.getHours();
自己動手試一試 »
getMinutes() 方法
getMinutes()
方法以數字(0-59)形式返回日期的分鐘。
示例
const d = new Date("2021-03-25");
d.getMinutes();
自己動手試一試 »
const d = new Date();
d.getMinutes();
自己動手試一試 »
getSeconds() 方法
getSeconds()
方法以數字(0-59)形式返回日期的秒。
示例
const d = new Date("2021-03-25");
d.getSeconds();
自己動手試一試 »
const d = new Date();
d.getSeconds();
自己動手試一試 »
getMilliseconds() 方法
getMilliseconds()
方法以數字(0-999)形式返回日期的毫秒。
示例
const d = new Date("2021-03-25");
d.getMilliseconds();
自己動手試一試 »
const d = new Date();
d.getMilliseconds();
自己動手試一試 »
getDay() 方法
getDay()
方法以數字(0-6)形式返回日期的星期幾。
注意
在 JavaScript 中,一週的第一天(第 0 天)是星期日。
世界上有些國家將一週的第一天視為星期一。
示例
const d = new Date("2021-03-25");
d.getDay();
自己動手試一試 »
const d = new Date();
d.getDay();
自己動手試一試 »
注意
可以使用名稱陣列和getDay()
來返回星期幾的名稱。
示例
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
自己動手試一試 »
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
自己動手試一試 »
getTime() 方法
getTime()
方法返回自 1970 年 1 月 1 日以來的毫秒數。
示例
const d = new Date("1970-01-01");
d.getTime();
自己動手試一試 »
const d = new Date("2021-03-25");
d.getTime();
自己動手試一試 »
const d = new Date();
d.getTime();
自己動手試一試 »
Date.now() 方法
Date.now()
返回自 1970 年 1 月 1 日以來的毫秒數。
示例
let ms = Date.now();
自己動手試一試 »
計算自 1970/01/01 以來的年數
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
let years = Math.round(Date.now() / year);
自己動手試一試 »
Date.now()
是 Date 物件的靜態方法。
你不能像 myDate.now()
這樣在日期物件上使用它。
語法始終是 Date.now()
。
UTC Date Get Methods
方法 | 等同於 | 描述 |
---|---|---|
getUTCDate() | getDate() | 返回 UTC 日期 |
getUTCFullYear() | getFullYear() | 返回 UTC 年份 |
getUTCMonth() | getMonth() | 返回 UTC 月份 |
getUTCDay() | getDay() | 返回 UTC 日期 |
getUTCHours() | getHours() | 返回 UTC 小時 |
getUTCMinutes() | getMinutes() | 返回 UTC 分鐘 |
getUTCSeconds() | getSeconds() | 返回 UTC 秒 |
getUTCMilliseconds() | getMilliseconds() | 返回 UTC 毫秒 |
UTC 方法使用 UTC 時間(協調世界時)。
UTC 時間與 GMT(格林威治標準時間)相同。
本地時間和 UTC 時間的差異最多可達 24 小時。
getTimezoneOffset() 方法
getTimezoneOffset()
方法返回本地時間和 UTC 時間之間的差值(以分鐘為單位)。