date-fns MMMM格式在俄语语言环境中被破坏

假设我们的日期为: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,1]).format('DD MMMM') => '1 ноября'

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

date-fns's GitHub repo

giftedhappy 回答:date-fns MMMM格式在俄语语言环境中被破坏

我在react-datepicker用例中针对此问题的解决方法是以cyrilic作为格式传递月份名称。

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

<DatePicker dateFormatCalendar={ getMonthName(new Date()) } />

format(new Date(2014,1,11),getMonthName(new Date(2014,11)))

在我的有限用例下工作

,

您需要将 MMMM 更改为 LLLL

,

使用 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。)

本文链接:https://www.f2er.com/3020559.html

大家都在问