Day.js 中文网

Day.js 中文网

  • 开发文档
  • Nodejs.cn 旗下网站

›插件

安装

  • 安装
  • Node.js
  • 浏览器
  • TypeScript
  • 下载

解析

  • 解析
  • 此刻
  • 字符串
  • 字符串 + 格式
  • Unix 时间戳(毫秒)
  • Unix 时间戳(秒)
  • 日期
  • 对象
  • 数组
  • UTC
  • Dayjs 克隆
  • 验证

取值+赋值

  • 取值+赋值
  • 毫秒
  • 秒
  • 分钟
  • 小时
  • 日期
  • 星期
  • 星期(区域设置感知)
  • ISO 星期
  • 年积日
  • 周
  • 周 (ISO)
  • 月份
  • 季度
  • 年份
  • 周年
  • 周年 (ISO)
  • 周数 (ISO)
  • 取值
  • 赋值
  • 最大值
  • 最小值

操作

  • 操作
  • 加法
  • 减法
  • 时间开端
  • 时间终端
  • 当地时间
  • UTC
  • UTC 偏移

显示

  • 显示
  • 格式
  • 从此刻到过去的时间
  • 从 X 到过去的时间
  • 从此刻到未来的时间
  • 从 X 到未来的时间
  • 日历时间
  • 时间差
  • Unix 时间戳(毫秒)
  • Unix 时间戳
  • 月份的天数
  • 作为 Javascript 日期
  • 作为数组
  • 作为 JSON
  • 作为 ISO 8601 字符串
  • 作为对象
  • 作为字符串

查询

  • 查询
  • 是否之前
  • 是否相同
  • 是否之后
  • 是否相同或之前
  • 是否相同或之后
  • 是否之间
  • 是否 Dayjs
  • 是否闰年

国际化

  • 国际化
  • 在 NodeJS 中加载语言环境
  • 在浏览器中加载语言环境
  • 全局地更改语言环境
  • 局部地更改语言环境
  • 检查当前 Day.js 语言环境
  • 列出当前区域设置的月份和工作日
  • 访问特定于语言环境的功能

插件

  • 插件
  • 在 NodeJS 中加载插件
  • 在浏览器中加载插件
  • AdvancedFormat
  • ArraySupport
  • BadMutable
  • BigIntSupport
  • BuddhistEra
  • 日历
  • CustomParseFormat
  • DayOfYear
  • DevHelper
  • Duration
  • IsBetween
  • IsLeapYear
  • IsSameOrAfter
  • IsSameOrBefore
  • IsToday
  • IsTomorrow
  • IsYesterday
  • IsoWeek
  • IsoWeeksInYear
  • LocaleData
  • LocalizedFormat
  • MinMax
  • ObjectSupport
  • PluralGetSet
  • PreParsePostFormat
  • QuarterOfYear
  • RelativeTime
  • Timezone
  • ToArray
  • ToObject
  • UpdateLocale
  • UTC
  • weekOfYear
  • WeekYear
  • Weekday

定制

  • 定制
  • 月份名称
  • 月份缩写
  • 星期名称
  • 星期缩写
  • 最小星期缩写
  • 相对时间
  • 日历

时长

  • 时长
  • 创建
  • 克隆
  • 人性化
  • 格式
  • 毫秒数
  • 秒数
  • 分钟数
  • 小时数
  • 天数
  • 周数
  • 月数
  • 年数
  • 添加时间
  • 减去时间
  • 将时长与时间差一起使用
  • 作为时间单位
  • 获取时间单位
  • 作为 JSON
  • 是否时长
  • 作为 ISO 8601 字符串
  • 语言环境

时区

  • 时区
  • 在区域中解析
  • 转换为区域
  • 猜测用户区域
  • 设置默认时区

Timezone

Timezone 添加了 dayjs.tz .tz .tz.guess .tz.setDefault API 以在时区之间解析或显示。

¥Timezone adds dayjs.tz .tz .tz.guess .tz.setDefault APIs to parse or display between time zones.

var utc = require("dayjs/plugin/utc");
// import utc from 'dayjs/plugin/utc' // ES 2015

var timezone = require("dayjs/plugin/timezone"); // dependent on utc plugin
// import timezone from 'dayjs/plugin/timezone' // ES 2015

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

猜测用户时区

¥Guessing the user timezone

dayjs.tz.guess();

在时区中解析

¥Parsing in a timezone

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

转换为时区

¥Converting to a timezone

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

设置/重置默认时区('tz' 使用)

¥Set / reset the default timezone (used by 'tz')

// Setting the default timezone
dayjs.tz.setDefault("America/New_York");

// Resetting the default timezone to the system timezone
dayjs.tz.setDefault();

与时刻的差异

¥Differences to moment

使用默认时区使 moment(dateValue) 使用该时区(但 moment.tz(dateValue, timezone) 仍然需要第二个参数)。

¥Using the default timezone makes moment(dateValue) use this timezone (but moment.tz(dateValue, timezone) still requires the second parameter).

但 dayjs(dateValue) 始终使用本地时区,即使使用 dayjs.tz.setDefault;只有 dayjs.tz(dateValue)(没有第二个参数)使用默认时区。

¥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.

← RelativeTimeToArray →
Day.js v1.11 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站