Dayjs 自定义语言环境抛出格式化异常
Dayjs custom locale throws Formatting exceptions
我想创建一个 自定义语言环境 并将其与 DayJs 一起使用。 docs
中提到了该过程
但是,当我按照步骤创建自己的语言环境时,我无法格式化之后的日期。
这里是 JSFiddle 检查相同。
https://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>
您需要在 locale 下导入 https://unpkg.com/browse/dayjs@1.8.17/, temporarily, looking at the script the locale
object was missing few properties like monthsShort
(idea take from 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('_')
}
};
我想创建一个 自定义语言环境 并将其与 DayJs 一起使用。 docs
中提到了该过程但是,当我按照步骤创建自己的语言环境时,我无法格式化之后的日期。
这里是 JSFiddle 检查相同。 https://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>
您需要在 locale 下导入 https://unpkg.com/browse/dayjs@1.8.17/, temporarily, looking at the script the locale
object was missing few properties like monthsShort
(idea take from 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('_')
}
};