教程 dayjs tutorial

自定义

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

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

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

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

提示

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

javascript
dayjs.extend(updateLocale);

dayjs.updateLocale("en", {
  /**/
});

Day.js 的语言对象模版。

javascript
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)

javascript
import dayjs from 'dayjs'

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

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

export default locale