教程 dayjs tutorial

UTC

默认情况下,Day.js 会把时间解析成本地时间。

如果想使用 UTC 时间,您可以调用 dayjs.utc() 而不是 dayjs()。

在 UTC 模式下,所有显示方法将会显示 UTC 时间而非本地时间。

提示

这依赖 UTC 插件,才能正常运行

javascript
dayjs.extend(utc);

// 默认是当地时间
dayjs().format(); //2019-03-06T08:00:00+08:00
// UTC 时间
dayjs.utc().format(); // 2019-03-06T00:00:00Z

此外,在 UTC 模式下, 所有 getters 和 setters 将使用 Date#getUTC* 和 Date#setUTC* 方法而不是 Date#get* 和 Date#set* 方法。

javascript
dayjs.utc().seconds(30).valueOf(); // => new Date().setUTCSeconds(30)
dayjs.utc().seconds(); // => new Date().getUTCSeconds()

要在本地时间和 UTC 时间之间切换,您可以使用 dayjs#utc 或 dayjs#local。