Timezone
Timezone 插件添加了 dayjs.tz .tz .tz.guess .tz.setDefault API,在时区之间解析或显示。
javascript
var utc = require("dayjs/plugin/utc");
var timezone = require("dayjs/plugin/timezone"); // dependent on utc plugin
dayjs.extend(utc);
dayjs.extend(timezone);
const timestamp = "2014-06-01 12:00";
const tz = "America/New_York";
const dayjsLocal = dayjs(timestamp); //assumes UTC
//dayjsLocal.toISOString() -> 2014-06-01T12:00:00.000Z
//dayjsLocal.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T12:00:00
const dayjsAmerica = dayjsLocal.tz(tz); //existing time treated as UTC
//dayjsAmerica.toISOString() -> 2014-06-01T12:00:00.000Z
//dayjsAmerica.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T08:00:00
const dayjsAmericaKeep = dayjsLocal.tz(tz, true); //existing time treated as local time
//dayjsAmericaKeep.toISOString() -> 2014-06-01T16:00:00.000Z
//dayjsAmericaKeep.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T12:00:00
推测用户时区
javascript
dayjs.tz.guess();
解析时区的时间
javascript
const d1 = dayjs.tz("2013-11-18 11:55", "Asia/Taipei");
d1.format(); // => 2013-11-18T11:55:00+08:00
d1.toISOString(); // => 2013-11-18T03:55:00.000Z
转换至目标时区
javascript
const d2 = dayjs.utc("2013-11-18 11:55").tz("Asia/Taipei");
d2.format(); // => 2013-11-18T19:55:00+08:00
d2.toISOString(); // => 2013-11-18T11:55:00.000Z
设置/重置默认时区
javascript
// Setting the default timezone
dayjs.tz.setDefault("America/New_York");
// Resetting the default timezone to the system timezone
dayjs.tz.setDefault();
和 Moment.js 的差异
Using the default timezone makes moment(dateValue)
use this timezone (but moment.tz(dateValue, timezone)
still requires the second parameter).
But dayjs(dateValue)
always uses the local timezone, even if dayjs.tz.setDefault
is used; only dayjs.tz(dateValue)
(without second parameter) uses the default timezone.