UTC
UTC 插件新增了 .utc
、.local
和 .isUTC
API,用於以 UTC 時間處理日期和時間。
javascript
var utc = require('dayjs/plugin/utc');
// import utc from 'dayjs/plugin/utc' // ES 2015
dayjs.extend(utc);
// 預設使用本地時間
dayjs().format(); // 2019-03-06T17:11:55+08:00
// UTC 模式
dayjs.utc().format(); // 2019-03-06T09:11:55Z
// 將本地時間轉換為 UTC 時間
dayjs().utc().format(); // 2019-03-06T09:11:55Z
// 在 UTC 模式下,所有顯示方法都將以 UTC 時間顯示,而不是本地時間。
// 所有的 getter 和 setter 方法將在內部使用 Date#getUTC* 和 Date#setUTC* 方法,而不是 Date#get* 和 Date#set* 方法。
dayjs.utc().isUTC(); // true
dayjs.utc().local().format(); // 2019-03-06T17:11:55+08:00
dayjs.utc('2018-01-01', 'YYYY-MM-DD'); // 使用 CustomParseFormat 插件
預設情況下,Day.js 使用本地時間進行解析和顯示。
如果需要在 UTC 時間進行解析或顯示,可以使用 dayjs.utc()
取代 dayjs()
。
dayjs.utc dayjs.utc(dateType?: string | number | Date | Dayjs, format?: string)
返回一個 UTC 模式的 Dayjs
物件。
設定為 UTC 時間 .utc()
返回一個複製的 Dayjs
物件,並將其設定為使用 UTC 時間。
設定為本地時間 .local()
返回一個複製的 Dayjs
物件,並將其設定為使用本地時間。
設定 UTC 偏移量 .utcOffset()
返回一個具有新的 UTC 偏移量的複製 Dayjs
物件。
檢查是否為 UTC 模式 .isUTC()
返回一個布林值,指示目前 Dayjs
物件是否處於 UTC 模式。