UTC
預設情況下,Day.js 使用本地時間進行日期時間的解析和顯示。
若需要使用 UTC 時間進行日期時間的解析或顯示,可以使用 dayjs.utc()
取代 dayjs()
。
在 UTC 模式下,所有顯示方法都會以 UTC 時間顯示,不再使用本地時間。
TIP
此功能需要 UTC 插件的支援。
js
dayjs.extend(utc);
// 預設為本地時間
dayjs().format(); //2019-03-06T08:00:00+08:00
// UTC 模式
dayjs.utc().format(); // 2019-03-06T00:00:00Z
此外,在 UTC 模式下,所有的 getter 和 setter 方法都會在內部使用 Date#getUTC*
和 Date#setUTC*
及 Date#setUTC*
方法,而非 Date#get*
和 Date#set*
方法。
js
dayjs.utc().seconds(30).valueOf(); // => new Date().setUTCSeconds(30)
dayjs.utc().seconds(); // => new Date().getUTCSeconds()
若要從 UTC 時間切換回本地時間,可以使用 dayjs#utc 或 dayjs#local。