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#get*
和 Date#set*
方法。
js
dayjs.utc().seconds(30).valueOf(); // => new Date().setUTCSeconds(30)
dayjs.utc().seconds(); // => new Date().getUTCSeconds()
要从 UTC 时间切换回本地时间,可以使用 dayjs#utc 或 dayjs#local。