gpt4 book ai didi

javascript - Dayjs 自定义语言环境抛出格式异常

转载 作者:行者123 更新时间:2023-12-02 22:19:54 25 4
gpt4 key购买 nike

我想创建一个自定义区域设置并将其与DayJs一起使用。 docs 中提到了该过程

但是,当我按照该过程并创建自己的区域设置时,我无法格式化之后的日期。

这是用于检查相同内容的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>

最佳答案

您需要导入 https://unpkg.com/browse/dayjs@1.8.17/ 中提到的所有文档暂时,查看脚本,locale 对象缺少一些属性,例如 monthsShort (想法取自 locale 下的 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('_')
}
};

关于javascript - Dayjs 自定义语言环境抛出格式异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com