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。