【发布时间】:2020-08-19 09:04:18
【问题描述】:
我在我的 SPA 应用程序中使用了Momentjs。
我的初衷是在我的部分页面使用calendarFormat自定义我的日历,在其他部分使用默认的calendarFormat。
不幸的是,我不知道如何设置默认的 calendarFormat。
还有什么想法?
【问题讨论】:
标签: javascript typescript web momentjs single-page-application
我在我的 SPA 应用程序中使用了Momentjs。
我的初衷是在我的部分页面使用calendarFormat自定义我的日历,在其他部分使用默认的calendarFormat。
不幸的是,我不知道如何设置默认的 calendarFormat。
还有什么想法?
【问题讨论】:
标签: javascript typescript web momentjs single-page-application
Ciao,你可以定义一个defaultCalendar 对象并在你想显示默认日历格式时使用它:
let defaultCalendar = {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] dddd',
sameElse: 'DD/MM/YYYY'
};
let anotherCalendar = {
sameDay: 'DD/MM/YYYY',
nextDay: 'DD/MM/YYYY',
nextWeek: 'DD/MM/YYYY',
lastDay: 'DD/MM/YYYY',
lastWeek: 'DD/MM/YYYY',
sameElse: 'DD/MM/YYYY'
};
console.log(moment().calendar(null, defaultCalendar));
console.log(moment().calendar(null, anotherCalendar));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
【讨论】:
我想出了一个巧妙的解决方案:
const defaultCalendarFormat = moment.calendarFormat;
moment.calendarFormat = myCalendarFormat;
moment.calendarFormat = defaultCalendarFormat;
【讨论】: