0

我想创建一个自定义语言环境并将其与DayJs一起使用。并且在文档中提到了该过程

但是,当我按照程序创建自己的语言环境时,我无法格式化之后的日期。

这是检查相同内容的JSFiddlehttps://jsfiddle.net/5o4pwbtc/

这是相同的GitHub问题:https ://github.com/iamkun/dayjs/issues/746

/*  like mentioned here
https://github.com/iamkun/dayjs/blob/dev/docs/en/I18n.md#installation
*/
const locale = {
  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: {
    name: 'en',
    future: '%s',
    past: '%s',
    s: 'now',
    m: 'a minute ago',
    mm: '%d minutes ago',
    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'
  }
}

dayjs.locale(locale);

dayjs(1575872723701).format() 
// Uncaught TypeError: Cannot read property '10' of undefined
<script src="https://unpkg.com/dayjs@1.8.17/dayjs.min.js"></script>

4

1 回答 1

1

您需要暂时导入https://unpkg.com/browse/dayjs@1.8.17/上提到的所有文档,查看该locale对象缺少一些属性的脚本,例如monthsShort(从en.js语言环境中获取的想法):

const locale = {
  formats: {
    ...
    monthsShort: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
    months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
    weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
    weekdaysShort: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_')
 }
};
于 2019-12-10T06:36:22.510 回答