How to format date with the name of the month in Portuguese and with capital letter at the Moment.js?

I need to format a date with Moment.js like this:

10 de Dezembro/2018.

I tried like this: "DD [de] MMMM/YYYY"

But the month gets the 1 lowercase letter. What's wrong?

Author: hkotsubo, 2018-12-10

2 answers

As already stated in the answer of Guilherme and in the comments , the names of the months in Portuguese begin with lowercase letter .

But if you still want them to start with capital letters, an alternative is to download the version of Moment.js com local, and then use updateLocale, passing month names the way you need:

moment.locale('pt'); // setar o locale para "pt" (Português)

// por padrão, o nome do mês é com letra minúscula
console.log(moment([2018, 11, 10]).format('DD [de] MMMM/YYYY')); // 10 de dezembro/2018

// mudar os nomes dos meses para o locale "pt"
moment.updateLocale('pt', {
    months : [
        "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho",
        "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"
    ]
});

// agora o nome do mês começa com maiúscula
console.log(moment([2018, 11, 10]).format('DD [de] MMMM/YYYY')); // 10 de Dezembro/2018
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>

In the example above I used moment([2018, 11, 10]) to create the date from" December 10, 2018", but you can switch to moment() to use the current date, for example. Notice that when passes an array , the months are indexed to zero (January is zero, February is 1, etc), so the value 11 for the month of December.


Do not use locales

The version with locales has the data of several languages, and (Consulting today on the official website) it has 66KB.

If you do not want to load data from multiple languages you will not use, an alternative is to use the version without locales (currently with 16KB ) and create your own locale:

// criar o locale para "pt" (Português)
moment.locale('pt', {
    months : ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
}); 

// agora o nome do mês começa com maiúscula
console.log(moment([2018, 11, 10]).format('DD [de] MMMM/YYYY')); // 10 de Dezembro/2018
<script src="https://momentjs.com/downloads/moment.min.js"></script>

In this example I set only the month names, but there are several other parameters to set when creating a locale (such as abbreviated month names, weekday names, etc). see documentation for the full list of fields to set.

 6
Author: hkotsubo, 2018-12-11 10:55:20

You can do something like this to make the first letter of the month uppercase.

let data = moment(...).format("MMM")
data = data[0].toUpperCase() + data.substr(1)

But there are good reasons not to do so. In Portuguese, the names of the months are not capitalized. Many languages do not capitalize on the names of their months or days of the week, including Spanish, French and Italian.

Each language file in moment.js is "owned" by at least one native speaker of the language. In general, you should not try to correct capitalizations on your own code. If you feel that there is an error in a specific locality, open a issue and wait to be analyzed and corrected if necessary.

Note from the developers of Moment about this.

We had some requests to provide capitalized versions alternatives, to be used in cases of exception of 1) the beginning of sentence, or 2) when one is alone as in the headings of column. The possibility of capitalizing or not (especially in the second case) it can vary significantly between languages. To from now on, the Moment does not offer any distinction and always points to the generic case.

 5
Author: Guilherme Rigotti, 2018-12-10 19:04:24