【问题标题】:How can I turn the calendarFormat of Momentjs to the default value?如何将 Momentjs 的 calendarFormat 设置为默认值?
【发布时间】:2020-08-19 09:04:18
【问题描述】:

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

还有什么想法?

【问题讨论】:

    标签: javascript typescript web momentjs single-page-application


    【解决方案1】:

    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>

    【讨论】:

      【解决方案2】:

      我想出了一个巧妙的解决方案:

      const defaultCalendarFormat = moment.calendarFormat;
      moment.calendarFormat = myCalendarFormat;
      moment.calendarFormat = defaultCalendarFormat;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-26
        • 2011-08-31
        相关资源
        最近更新 更多