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 字符串
  • 语言环境

时区

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

相对时间

Locale#relativeTime 应该是 dayjs#from 的替换字符串的对象。

¥Locale#relativeTime should be an object of the replacement strings for dayjs#from.

This requires the UpdateLocale plugin to work

dayjs.extend(updateLocale)

dayjs.updateLocale('en', {
  relativeTime: {
    future: "in %s",
    past: "%s ago",
    s: 'a few seconds',
    m: "a minute",
    mm: "%d minutes",
    h: "an hour",
    hh: "%d hours",
    d: "a day",
    dd: "%d days",
    M: "a month",
    MM: "%d months",
    y: "a year",
    yy: "%d years"
  }
})

Locale#relativeTime.future 指未来日期的前缀/后缀。Locale#relativeTime.past 指过去日期的前缀/后缀。

¥Locale#relativeTime.future refers to the prefix/suffix for future dates. Locale#relativeTime.past refers to the prefix/suffix for past dates.

对于所有其他字符,单字符表示单数,双字符表示复数。

¥For all others, a single character refers to the singular, and a double character refers to the plural.

额外的令牌处理

¥Additional token processing

如果语言环境需要对令牌进行额外处理,则可以将令牌设置为具有以下签名的函数,而不是字符串。该函数应返回一个字符串。

¥If a locale requires additional processing for a token, it can set the token as a function with the following signature instead of a string. The function should return a string.

relativeTime: {
  ...,
  yy: function (number, withoutSuffix, key, isFuture) {
    return string;
  }
}

number 参数指的是该键的单位数。对于 m,数字是分钟数等。

¥The number argument refers to the number of units for that key. For m, the number is the number of minutes, etc.

如果令牌不带后缀显示,则 withoutSuffix 参数将为 true,如果带后缀显示,则为 false。(倒置逻辑的原因是因为默认行为是用后缀显示。)

¥The withoutSuffix argument will be true if the token will be displayed without a suffix, and false if it will be displayed with a suffix. (The reason for the inverted logic is because the default behavior is to display with the suffix.)

key 参数引用 Locale#relativeTime 对象中的替换键。(如 s m mm h 等)

¥The key argument refers to the replacement key in the Locale#relativeTime object. (eg. s m mm h, etc.)

如果要使用未来的后缀/前缀,则 isFuture 参数将为真;如果要使用过去的前缀/后缀,则为假。

¥The isFuture argument will be true if it is going to use the future suffix/prefix and false if it is going to use the past prefix/suffix.

相对时间阈值和舍入

¥Relative Time thresholds and rounding

你可以在使用此插件更新其阈值和舍入配置时传递配置对象。

¥You can pass a config object while using this plugin to update its thresholds and rounding config.

var config = {
  thresholds: [{}],
  rounding: function
}
dayjs.extend(relativeTime, config)

thresholds 是 Object 的 Array,当单位被视为分钟、小时等时定义。例如,默认情况下超过 45 秒被视为一分钟,超过 22 小时被视为一天等等。要更改这些,你可以像这样传递一个新的 thresholds。

¥thresholds is an Array of Object defined when a unit is considered a minute, an hour and so on. For example, by default more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. To change those you can pass a new thresholds like this.

// strict thresholds
var thresholds = [
  { l: 's', r: 1 },
  { l: 'm', r: 1 },
  { l: 'mm', r: 59, d: 'minute' },
  { l: 'h', r: 1 },
  { l: 'hh', r: 23, d: 'hour' },
  { l: 'd', r: 1 },
  { l: 'dd', r: 29, d: 'day' },
  { l: 'M', r: 1 },
  { l: 'MM', r: 11, d: 'month' },
  { l: 'y', r: 1 },
  { l: 'yy', d: 'year' }
]

你还可以添加自己的阈值键并相应地更新区域设置。

¥You can also add your own thresholds key and update locale accordingly.

var thresholds = [
  ...,
  { l: 'ss', r: 59, d: 'second' }
]
dayjs.updateLocale('en', {
  relativeTime: {
    ...,
    ss: "%d seconds"
  }
})

rounding 是一个 Function,用于在将数字提供给区域设置中指定的 relativeTime 格式字符串之前对其进行处理。要更改这些,你可以像这样传递一个新的 rounding。

¥rounding is an Function to process the number before supplying it to the relativeTime format string specified in the locale. To change those you can pass a new rounding like this.

// Math.round by default
var rounding = Math.floor
← 最小星期缩写日历 →
Day.js v1.11 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站