如何将 Momentjs 的 calendarFormat 设置为默认值?

How can I turn the calendarFormat of Momentjs to the default value?

我在 SPA 应用程序中使用了 Momentjs
我的初衷是在我的页面的一部分使用calendarFormat来自定义我的日历,而在其他部分使用默认的calendarFormat
不幸的是我不知道如何设置默认的日历格式。

和想法?

再见,你可以定义一个 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;