跳至主要内容

Date

在 JavaScript 中,Date 是一個內建的物件,用來處理日期和時間。 Date 物件是基於世界標準時間(UTC) 1970 年 1 月 1 日開始的毫秒數值(64 位浮點數)來儲存時間,不會受到 2038 年問題影響。

建立 Date 物件

new Date() 是 JavaScript 中用來建立 Date 物件的構造函數。

  • 無參數
const now = new Date();
console.log(now); // Tue Aug 27 2024 23:21:17 GMT+0800 (台北標準時間)
  • 時間戳 傳遞一個表示從 Unix 紀元(1970 年 1 月 1 日)起的毫秒數來創建 Date 物件。例如,1693123456000 毫秒對應於具體的日期和時間。
const timestamp = 1693123456000;
const date = new Date(timestamp);
console.log(date); // Sun Aug 27 2023 16:04:16 GMT+0800 (台北標準時間)
  • 日期字串 傳遞一個日期字串來創建 Date 物件。這個字串應該要能被 Date.parse() 方法解析(符合 IETF-compliant RFC 2822 timestamps 及 ISO8601 格式要求,例如:
warning

由於各家瀏覽器有所不同且具有差異性,因此非常不鼓勵使用 new Date()Date.parse() 來解析時間日期字串。

const dateStr = "2024-08-27T16:04:16.000Z";
const date = new Date(dateStr);
console.log(date); // Sun Aug 27 2023 16:04:16 GMT+0800 (台北標準時間)
  • 年、月、日、時、分、秒、毫秒
console.log(new Date(2024, 7, 27, 12, 34, 56, 789)); // Sun Jul 27 2024 12:34:56 GMT+0800 (台北標準時間)
console.log(new Date(2024, 7)); // Mon Jul 01 2024 00:00:00 GMT+0800 (台北標準時間)
console.log(new Date(2024)); // Mon Jan 01 2024 00:00:00 GMT+0800 (台北標準時間)

取得時間單位

timestamp

Date 物件的 getTime() 方法可以取得從 Unix 紀元(1970 年 1 月 1 日)起的毫秒數。

const now = new Date();
console.log(now.getTime()); // 1693123456000

// 或是使用 Date.now() 取得目前時間的 timestamp
console.log(Date.now()); // 1693123456000

年、月、日、星期、時、分、秒、毫秒

在 JavaScript 種,有兩種方法可以取得 Date 物件的時間單位:

  1. get 開頭的方法,例如 getFullYear()getMonth()getDate()getHours()getMinutes()getSeconds()getMilliseconds()
  2. getUTC 開頭的方法,例如 getUTCFullYear()getUTCMonth()getUTCDate()getUTCHours()getUTCMinutes()getUTCSeconds()getUTCMilliseconds()

兩種的差異在於 get 開頭的方法會根據當地時區取得時間,而 getUTC 開頭的方法則是取得** UTC 時間**。

const now = new Date("2024-08-27T00:00:00.000+08:00");

console.log(now.getFullYear()); // 2024
console.log(now.getMonth()); // 7,因為月份是從 0 開始計算
console.log(now.getDate()); // 27
console.log(now.getDay()); // 2,星期二
console.log(now.getHours()); // 0
console.log(now.getMinutes()); // 0
console.log(now.getSeconds()); // 0
console.log(now.getMilliseconds()); // 0

console.log(now.getUTCFullYear()); // 2024
console.log(now.getUTCMonth()); // 7
console.log(now.getUTCDate()); // 26,因為 UTC 時間比台灣時間早 8 小時,所以日期會比台灣時間少一天
console.log(now.getUTCDay()); // 1,星期一
console.log(now.getUTCHours()); // 16
console.log(now.getUTCMinutes()); // 0
console.log(now.getUTCSeconds()); // 0
console.log(now.getUTCMilliseconds()); // 0

時區

getTimezoneOffset() 是基於你現在執行程式的電腦的當地時區來計算。

const now = new Date("2024-08-27T15:21:17.000Z");

console.log(now.getTimezoneOffset()); // -480,因為台灣的時區是 UTC+8,所以差距是 -8 小時,也就是 -480 分鐘

const date1 = new Date("August 19, 1975 23:15:30 UTC+07:00");
const date2 = new Date("August 19, 1975 23:15:30 UTC-02:00");

// getTimezoneOffset() 會將時區轉換為 UTC 時區後再計算差距,因此兩個日期的時區不同,但 getTimezoneOffset() 的結果相同
console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset()); // true

字串轉換

在 JavaScript 中,有多種方法可以將 Date 物件轉換為字串:

  • toString():將 Date 物件轉換為當地時區的日期和時間字串。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toString()); // Sun Aug 27 2024 23:21:17 GMT+0800 (台北標準時間)
  • toUTCString():將 Date 物件轉換為 UTC 時區的日期和時間字串。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toUTCString()); // Sun, 27 Aug 2024 15:21:17 GMT
  • toISOString():將 Date 物件轉換為 ISO 8601 格式的字串。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toISOString()); // 2024-08-27T15:21:17.000Z
  • toDateString():將 Date 物件的日期部分轉換為字串。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toDateString()); // Sun Aug 27 2024
  • toTimeString():將 Date 物件的時間部分轉換為字串。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toTimeString()); // 23:21:17 GMT+0800 (台北標準時間)
  • toLocaleString():將 Date 物件轉換為當地時區的日期和時間字串,並根據當地化設定格式化。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toLocaleString()); // 2024/8/27 下午11:21:17
  • toLocaleDateString():將 Date 物件的日期部分轉換為字串,並根據當地化設定格式化。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toLocaleDateString()); // 2024/8/27
  • toLocaleTimeString():將 Date 物件的時間部分轉換為字串,並根據當地化設定格式化。
const now = new Date("2024-08-27T15:21:17.000Z");
console.log(now.toLocaleTimeString()); // 下午11:21:17

參考資料