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

时区

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

PreParsePostFormat

预解析/后格式化允许你在解析器之前处理输入并在格式化器之后处理字符串输出。基于 moment.js 中区域设置的类似行为。

¥Pre-parse / Post-format lets you process the input before the parser and process the string output after the formatter. Based on similar behavior for locales in moment.js.

注意:该插件需要在它之前导入 localeData 插件(因为它取决于它的功能)。

¥NOTE: this plugin requires the localeData plugin to be imported before it (as it depends on it's functionality).

注意:该插件还会影响相对时间插件,也是设计使然(模仿 moment.js 实现行为)。

¥NOTE: this plugin also affects the relative time plugin, also by design (mimics the moment.js implementaiton behavior).

使用示例

¥Sample usage

例如 特别是在 AR 语言环境中,它用于支持阿拉伯数字。

¥e.g. In the AR locale specifically, it is used to support Arabic numerals.

// Arabic [ar]
import dayjs from 'dayjs'
import preParsePostFormat from 'dayjs/plugin/preParsePostFormat'
dayjs.extend(preParsePostFormat)

const months = 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_')
const symbolMap = {
  1: '١',
  2: '٢',
  3: '٣',
  4: '٤',
  5: '٥',
  6: '٦',
  7: '٧',
  8: '٨',
  9: '٩',
  0: '٠'
}

const numberMap = {
  '١': '1',
  '٢': '2',
  '٣': '3',
  '٤': '4',
  '٥': '5',
  '٦': '6',
  '٧': '7',
  '٨': '8',
  '٩': '9',
  '٠': '0'
}

const locale = {
  name: 'ar',
  // ...
  preparse(string) {
    return string
      .replace(
        /[١٢٣٤٥٦٧٨٩٠]/g,
        match => numberMap[match]
      )
      .replace(/،/g, ',')
  },
  postformat(string) {
    return string
      .replace(/\d/g, match => symbolMap[match])
      .replace(/,/g, '،')
  },
  // ...
}
// ...

如果这还不够清楚,测试 还应该为你提供有关如何使用该插件的好主意;)。

¥The tests should also give you a good idea on how to use the plugin if this isn't clear enough ;).

← PluralGetSetQuarterOfYear →
  • 使用示例
Day.js v1.11 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站