How to format date with Moment.js?

3

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

10 de Dezembro/2018.

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

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

    
asked by anonymous 10.12.2018 / 19:53

2 answers

6

As I said in the Guilherme's answer and in comments , the names of the months in Portuguese begin with lowercase letter .

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

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>

IntheexampleaboveIusedmoment([2018,11,10])tocreatethedate"December 10, 2018", but you can switch to moment() to use the current date, for example. Notice that by passing an array , the months are indexed to zero (January is zero, February is 1, etc.) ), so the 11 value for the month of December.

Do not use the premises

A localized version has the data of several languages, and (referring today to < a href="https://momentjs.com/"> official site ) it has 66KB.

If you do not want to load data from multiple languages that you will not use, an alternative is to use the unplaced version (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>

InthisexampleIhavesetuponlythenamesofthemonths,butthereareseveralotherparameterstosetwhencreatingalocale(suchasshortenedmonthnames,weekdaynames,etc.). View the documentation for the complete list of fields to set.

    
10.12.2018 / 21:17
5

You can do something like this to leave 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. In Portuguese, the names of the months are not capitalized. Many languages do not capitalize the names of their months or days of the week, including Spanish, French, and Italian.

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

Moment developers note on this.

  

We had some requests to provide capitalized versions   alternatives, to be used in the exception cases of 1) the beginning of the   sentence, or 2) when you are alone as in the headings of the   columns. The possibility of capitalizing or not (especially in the   second case) can vary significantly between languages. THE   from now on, Moment makes no distinction and always   points to the generic case.

    
10.12.2018 / 19:59