【问题标题】:date-fns MMMM formatting is broken in Russian localedate-fns MMMM 格式在俄罗斯语言环境中被破坏
【发布时间】:2020-03-23 04:24:58
【问题描述】:

假设我们有以下日期:2019 年 11 月 1 日。

我只想显示月份名称,但MMMM 格式化结果为“ноября”,而我希望得到“ноябрь”。

moment.js(语言环境:ru)中可以正常工作:

// if format starts with month name
moment([2019, 11, 1]).format('MMMM') => 'ноябрь'

// if format doesn't start with month name
moment([2019, 11, 1]).format('DD MMMM') => '1 ноября'

我想切换到 moment.js,尽管我已经在使用严格依赖于 date-fns 的库。如何实现以俄语正确显示月份名称的格式?

date-fns's GitHub repo

【问题讨论】:

    标签: time momentjs datetime-format time-format date-fns


    【解决方案1】:

    我在 react-datepicker 用例中解决此问题的方法是使用西里尔文格式的月份名称。

    const getMonthName = (date) => {
        const months = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'];
        return months[date.getMonth()];
    };
    
    <DatePicker dateFormatCalendar={ getMonthName(new Date()) } />
    
    format(new Date(2014, 1, 11), getMonthName(new Date(2014, 1, 11)))
    

    在我有限的用例下工作

    【讨论】:

      【解决方案2】:

      您需要将 MMMM 更改为 LLLL

      【讨论】:

        【解决方案3】:

        使用LLLL,如date-fns format documentation page的示例所示。

        演示:

        const fns = require('date-fns');
        const { ru } = require('date-fns/locale');
        
        const now  = new Date(2019, 11, 1);
        
        console.log(fns.format(now, "LLLL", {locale: ru}));
        

        输出:

        декабрь
        

        ONLINE DEMO

        注意:点击在线演示页面右下角的Console。)

        【讨论】:

          猜你喜欢
          • 2018-04-26
          • 2019-01-03
          • 2021-09-17
          • 1970-01-01
          • 2023-01-05
          • 1970-01-01
          • 2021-12-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多