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

Asked

Viewed 3,118 times

3

I need to format a date with the Moment.js thus:

10 de Dezembro/2018.

I tried so: "DD [de] MMMM/YYYY"

But the month is down to 1 letter. What’s wrong?

  • 1

    Please post a part of your code, otherwise it is difficult to understand what is happening.

  • 1

    It is incorrect for the month name to begin with uppercase, at least in this format.

  • In English Moment writes the months starting with capital letters because there in the code is months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"). For Portuguese has two lines equal to this months:"janeiro_fevereiro_mar\xe7o_abril_maio_junho_julho_agosto_setembro_outubro_novembro_dezembro".split("_") if the file is hosted on your server, if you exchange these two lines for "Janeiro_Fevereiro_Mar\xe7o_Abril_Maio_Junho_Julho_Agosto_Setembro_Outubro_Novembro_Dezembro".split("_") will write the months starting with capital

  • Check out http://kithomepage.com/sos/momento-mes-maiuscula.html

2 answers

7


As already said in william’s response and in the comments, the names of the months in Portuguese begin with lower case letter.

But if you still want them to capitalize, an alternative is to download the version of Moment.js with locales, and then use updateLocale, passing the names of the months the way you need them:

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 above example I used moment([2018, 11, 10]) to create the "December 10, 2018" date, but you can switch to moment() to use the current date, for example. Note that pass an array, the months are indexed at zero (January is zero, February is 1, etc), so the value 11 for the month of December.


Do not use locales

To version with locales has the data of several languages, and (consulting today on official website) she owns 66KB.

If you do not want to upload data from multiple languages that you will not use, an alternative is to use the no locale 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>

In this example I have configured only month names, but there are several other parameters to be set when creating a locale (such as abbreviated month names, days of the week names, etc). View documentation for the complete list of fields to be set.

  • 1

    Thank You All, Solved with : // Moment import Moment from "Moment"; const pt = Moment.locale('pt', { months : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], }); export default pt;

5

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 English, month names 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 on moment.js is "property" of at least one native speaker of the language. In general, you should not attempt to correct the capitalizations in your own code. If you feel there is an error in a specific location, open a issue and wait to be reviewed and corrected if necessary.

Note from the Moment developers about this.

We had some requests to provide uppercase versions alternatives, to be used in cases of exception 1) the beginning of the sentence, or 2) when one is alone as in the headers of columns. Whether or not to capitalise (especially in second case) may vary significantly between languages. A From now on, Moment offers no distinction and always points to the generic case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.