Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercasing monthnames in momentjs

I changed moments locale by setting the following property:

moment.locale(chosenLocale);

Everything seems to work fine. I get the month names and the weekday names according to the selected locale. I also get correct calculation of weeknumber etc.

With default (english) locale i get month names like January, February, etc. I also get weekday names like Monday, Tuesday, etc. For some reason, with danish locale i get all these names lowercased. When formatting a simple weekday, i can just capitalize the first letter but for some more advanced formats where the name might swap order (January 1st vs. 1. Januar) i can't just uppercase the first letter.

I'm using this format to display the month name and day of month:

moment().format('dddd LL')

In danish i get 7. marts 2016 but i really want 7. Marts 2016. Keep in mind, i need the solution to work on all locales, so i can't hardcode the month names - or can I? I tried the following:

moment.locale(chosenLocale);
var __months = moment.months().map(function (m) { return m.toUpperCase() + "TEST"; });
moment.locale(chosenLocale, {
    months : __months
}); 

I would expect (for testing purposes) to get JANUARTEST for danish locale, but i get januartest which hints that the lower case is applied by the framework somewhere else. I also tried to set the months property to a function according the the api docs and then return the uppercase value of the cached month array, which same result as described.

Does anyone have a solution to this?

like image 362
Per Hornshøj-Schierbeck Avatar asked Mar 10 '16 10:03

Per Hornshøj-Schierbeck


People also ask

How do I get the current time in MomentJS?

To get the current date and time, just call javascript moment() with no parameters like so: var now = moment(); This is essentially the same as calling moment(new Date()) .

What is MomentJS used for?

MomentJS is a JavaScript library which helps is parsing, validating, manipulating and displaying date/time in JavaScript in a very easy way. This chapter will provide an overview of MomentJS and discusses its features in detail. Moment JS allows displaying of date as per localization and in human readable format.

What is MomentJS?

Moment. js is a stand-alone open-source JavaScript framework wrapper for date objects that eliminates native JavaScript date objects, which are cumbersome to use. Moment. js makes dates and time easy to display, format, parse, validate, and manipulate using a clean and concise API.

What is Moment () format?

Moment's format() method is what you use to convert a Moment object to a string. For example, here's how you would convert a YYYY-MM-DD string into a more human-readable format: const moment = require('moment'); const d = new Date('2019/06/01'); moment(d).format('MMMM d, YYYY'); // June 1, 2019.


4 Answers

For some reason, with danish locale i get all these names lowercased.

There's good reason for that. In Danish, the names of the months are not capitalized. Many languages do not capitalize the names of their months or weekdays, including Spanish, French, Italian, Russian, and others.

Each locale file in moment.js is "owned" by at least one native speaker of the language. In general, you should not try to correct the capitalizations in your own code. If you feel there's an error with a particular locale, open an issue and we can get input from the locale owner.

Note, we have had a few requests for providing alternate cased versions, to be used in the exception cases of 1) the start of the sentence, or 2) when standing alone as in column headers. Whether or not to capitalize these (especially in the second case) can vary significantly between languages. As of now, moment doesn't offer any distinction, and always aims for the generic case.

That said, I tried your code against the current version (2.12.0) and it does work, so perhaps you are on an older version that handled this differently. However, I do get a deprecation warning, because you should be using the updateLocale method to modify an existing locale, so your code should be changed as follows:

moment.updateLocale(chosenLocale, { months : __months });

Still, I advise against this for the reasons explained above.

like image 103
Matt Johnson-Pint Avatar answered Oct 20 '22 04:10

Matt Johnson-Pint


You can give CSS a try:

.selector .to .lowercase .element { text-transform: capitalize; }

If you have that selector, that should capitalize any string.

like image 25
agbb Avatar answered Oct 20 '22 06:10

agbb


Just use .toUpperCase()

Eg:

<Text>TARGET DATE: {Moment("10-jan-20").format('DD MMMM YYYY').toUpperCase()}</Text>

That returns: "TARGET DATE: 10 JANUARY 2020"

like image 20
Wasantha Wijayaratna Avatar answered Oct 20 '22 04:10

Wasantha Wijayaratna


I'm having the same issue in Spanish as it is reported in French by Charles Martin. Who ever has translated the Months and Days names in spanish by using all in lowercase should have opted to make the 1st letter in UpperCase like as in English.

With this fix it's relatively easy to convert all .toLowerCase() If we need it.

To convert just the 1st letter to uppercase for the week and month inside the .format('dddd, DD [de] MMMM') is not possible and to make it outside push us to use another function to split , uppercase 1st letter, and joint to process the result.

Until a fix is implemented, I will stick with the solution proposed to update the locale on the fly.

moment.locale('es');
var meses = 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_');
var semanas = 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_');
moment.updateLocale('es', { months : meses, weekdays : semanas });

There are other things to replace if you need to. Just open the moment-with-locales.js file and search hooks.defineLocale('es' to see all the properties and use the method explained above to change those from outside the library.

like image 38
Gerardo Verrone Avatar answered Oct 20 '22 04:10

Gerardo Verrone