Open In App

Moment.js Customize Month Names

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the moment.js Customize Month Names in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.

The moment.updateLocale() function allows us to add month names to the locale customization. It helps us to fulfill the need for more processing to calculate the month’s name. The month can also be set using a string of the month’s full name or shorthand, they are customizable according to the user’s need.

Syntax:

moment.updateLocale('en', {
    months: String[]
});

OR

moment.updateLocale('en', {
    months: Function
});

OR

moment.updateLocale('en', {
    months: {
        format: String[],
        standalone: String[]
    }
});

Parameters:

  • months: The month that has to be set for the Moment object. It is an optional parameter.

Returns: This method returns the current month of the Moment.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory. For more details, please refer to this link.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

Example 1: In this example, we are simply using the updatelocale() function the name of the months in the short form of each month, and getting it at the end as the output.

Javascript




// Importing moment module
const moment = require('moment');
let localeData = moment.updateLocale('en', {
    months: [
        "Jan", "Feb", "Mar", "Apr", "May", "June", "July",
        "Aug", "Sept", "Oct", "Nov", "Dec"
    ]
});
let m = localeData.months();
console.log("The customized month name are here:", m);


Output:

The customized month name are here: [
      'Jan',  'Feb', 'Mar',
      'Apr',  'May', 'June',
      'July', 'Aug', 'Sept',
      'Oct',  'Nov', 'Dec'
]

Example 2: Here, in this example, we have customized the month name accordingly, we have added gfg by passing the name required in the nominative and subjective as a single string to the end of each month name and at the end getting the name of the current month in JavaScript.

Javascript




const moment = require('moment');
  
let localeData = moment.updateLocale('en', {
    nominative:
        'Jangfg_Febgfg_Margfg_Aprgfg_Maygfg_Junegfg_Julygfg_Auggfg_Septgfg_Octgfg_Novgfg_Decgfg'.split('_'),
    subjective:
        'Jangfg_Febgfg_Margfg_Aprgfg_Maygfg_Junegfg_Julygfg_Auggfg_Septgfg_Octgfg_Novgfg_Decgfg'.split('_'),
    months: function (momentToFormat, format) {
        if (/^MMMM/.test(format)) {
            console.log(this._nominative);
            return this._nominative[momentToFormat.month()];
        } else {
            return this._subjective[momentToFormat.month()];
        }
    }
});
let m = localeData.months(moment(), "MMMM");
console.log("The Current customized month name is:", m);


Output:

The Current customized month name is: Septgfg

Reference: https://momentjs.com/docs/#/customization/month-names/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads