Day.js

Day.js

  • 文档
  • GitHub
  • Languages icon中文
    • English
    • Русский
    • 帮助我们翻译

›自定义

安装

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

解析

  • 解析
  • 当前时间
  • 字符串
  • 字符串 + 格式
  • Unix 时间戳 (毫秒)
  • Unix 时间戳 (秒)
  • Date 对象
  • 对象
  • 数组
  • UTC
  • Dayjs 复制
  • 验证

取值/赋值

  • 取值/赋值
  • Millisecond
  • Second
  • Minute
  • Hour
  • Date of Month
  • Day of Week
  • Day of Week (Locale Aware)
  • ISO Day of Week
  • 每年中的第几天
  • Week of Year
  • Week of Year (ISO)
  • Month
  • Quarter
  • Year
  • Week Year
  • Week Year (ISO)
  • Weeks In Year (ISO)
  • Get
  • Set
  • Maximum
  • Minimum

操作

  • 操作
  • Add
  • Subtract
  • Start of Time
  • End of Time
  • Local
  • UTC
  • UTC offset

显示

  • 显示
  • Format
  • Time from now
  • Time from X
  • Time to now
  • Time to X
  • Calendar-time
  • Difference
  • Unix 时间戳 (毫秒)
  • Unix 时间戳
  • 月份中天数
  • As Javascript Date
  • As Array
  • As JSON
  • As ISO 8601 String
  • As Object
  • As String

查询

  • 查询
  • Is Before
  • Is Same
  • Is After
  • Is Same or Before
  • Is Same or After
  • Is Between
  • Is a Dayjs
  • Is Leap Year

国际化 (i18n)

  • 国际化 (i18n)
  • 加载语言配置 (NodeJS)
  • 加载语言配置 (浏览器)
  • 改变语言配置 (全局)
  • 改变语言配置 (当前实例)
  • 查看当前语言配置
  • 列出当前语言的月份和周
  • 获取语言配置的属性

插件

  • 插件
  • 加载插件 (NodeJS)
  • 加载插件 (浏览器)
  • AdvancedFormat
  • ArraySupport
  • BadMutable
  • BigIntSupport
  • BuddhistEra
  • Calendar
  • 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

自定义

  • 自定义
  • Month Names
  • Month Abbreviations
  • Weekday Names
  • Weekday Abbreviations
  • Minimal Weekday Abbreviations
  • Relative Time
  • Calendar

时长 (Duration)

  • 时长
  • Creating
  • Clone
  • Humanize
  • Format
  • Milliseconds
  • Seconds
  • Minutes
  • Hours
  • Days
  • Weeks
  • Months
  • Years
  • Add Time
  • Subtract Time
  • Using Duration with Diff
  • As Unit of Time
  • Get Unit of Time
  • As JSON
  • Is a Duration
  • As ISO 8601 String
  • Locale

时区

  • 时区
  • 解析时区
  • 转换到对应时区
  • 用户当前时区
  • 设置默认时区
Translate

自定义

想自定义 Day.js 也很容易。

您可以创建一个新的语言配置。

var localeObject = {...} // Day.js 语言对象,下面有详述
dayjs.locale('en-my-settings', localeObject);

更新一个已有的语言配置。

这依赖 UpdateLocale 插件,才能正常运行

dayjs.extend(updateLocale)

dayjs.updateLocale('en', {
  /**/
})

Day.js 的语言对象模版。

const localeObject = {
  name: 'es', // name String
  weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
  weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
  weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
  weekStart: 1, // OPTIONAL, set the start of a week. If the value is 1, Monday will be the start of week instead of Sunday。
  yearStart: 4, // OPTIONAL, the week that contains Jan 4th is the first week of the year.
  months: 'Enero_Febrero ... '.split('_'), // months Array
  monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
  ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
  formats: {
    // abbreviated format options allowing localization
    LTS: 'h:mm:ss A',
    LT: 'h:mm A',
    L: 'MM/DD/YYYY',
    LL: 'MMMM D, YYYY',
    LLL: 'MMMM D, YYYY h:mm A',
    LLLL: 'dddd, MMMM D, YYYY h:mm A',
    // lowercase/short, optional formats for localization
    l: 'D/M/YYYY',
    ll: 'D MMM, YYYY',
    lll: 'D MMM, YYYY h:mm A',
    llll: 'ddd, MMM D, YYYY h:mm A'
  },
  relativeTime: {
    // relative time format strings, keep %s %d as the same
    future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
    past: '%s ago',
    s: 'a few seconds',
    m: 'a minute',
    mm: '%d minutes',
    h: 'an hour',
    hh: '%d hours', // e.g. 2 hours, %d been replaced with 2
    d: 'a day',
    dd: '%d days',
    M: 'a month',
    MM: '%d months',
    y: 'a year',
    yy: '%d years'
  },
  meridiem: (hour, minute, isLowercase) => {
    // OPTIONAL, AM/PM
    return hour > 12 ? 'PM' : 'AM'
  }
}

Day.js 的语言文件模板。 (例 dayjs/locale/es.js)

import dayjs from 'dayjs'

const locale = { ... } // Day.js 的语言对象.

dayjs.locale(locale, null, true) // load locale for later use

export default locale
← WeekdayMonth Names →
Day.js
Community
GitHubGitter🇨🇳 China Mirror 中国镜像站点Sitemap
Copyright © 2023 Day.js