UTC
默认情况下,Day.js 以当地时间解析和显示。
如果要解析或显示 UTC 格式的日期时间,可以使用 dayjs.utc()
而不是 dayjs()
。
在 UTC 模式下,所有显示方式都将以 UTC 时间显示,而不是本地时间。
需要配合 UTC 插件才能工作
dayjs.extend(utc)
// default local time
dayjs().format() //2019-03-06T08:00:00+08:00
// UTC mode
dayjs.utc().format() // 2019-03-06T00:00:00Z
此外,在 UTC 模式下,所有 getter 和 setter 将在内部使用 Date#getUTC*
和 Date#setUTC*
方法,而不是 Date#get*
和 Date#set*
方法。
dayjs.utc().seconds(30).valueOf()// => new Date().setUTCSeconds(30)\
dayjs.utc().seconds()// => new Date().getUTCSeconds()
要从 UTC 切换到本地时间,你可以使用 dayjs#utc 或 dayjs#local。