Open In App

Moment.js Customize Month Abbreviations

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

The moment.updateLocale() function helps customize the month abbreviations based on the locale set. 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, which is customizable according to the user’s need.

Syntax:

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

Parameters:

  • monthsShort: The monthsShort that has to be set for the Moment object. it is an optional parameter.

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 moment.updatelocale() function the month’s name in the short form of each month in its abbreviation and gets at the end as the output.

Javascript




// Importing moment module
const moment = require('moment');
  
let localeData = moment.updateLocale('en', {
    monthsShort:
        ["Jan", "Feb", "Mar", "Apr", "May",
            "Jun", "Jul", "Aug", "Sep", "Oct",
            "Nov", "Dec"]
});
  
let gfg = localeData.monthsShort();
console.log("The Month Abbreviation array:", gfg);


Output:

The Month Abbreviation array: [
  'Jan', 'Feb', 'Mar',
  'Apr', 'May', 'Jun',
  'Jul', 'Aug', 'Sep',
  'Oct', 'Nov', 'Dec'
]

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

Javascript




// Importing moment module
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('_'),
    monthsShort:
        function (momentToFormat, format) {
            if (/^MMMM/.test(format)) {
                console.log(this._nominative);
                return this._nominative[momentToFormat.month()];
            } else {
                return this._subjective[momentToFormat.month()];
            }
        }
});
let gfg = localeData.monthsShort(moment(), "MMMM");
console.log("The Month Abbreviation of the current month:", gfg);


Output:

The Month Abbreviation of the current month: SeptGFG

Example 3: In this example, we have customized the month’s name accordingly. In this, we have added gfg by passing the name required in the nominative and subjective as a single string to the end of each month’s name written in small letters and, in the end, getting the current month’s name in javascript.

Javascript




// Importing moment module
const moment = require('moment');
  
let localeData = moment.updateLocale('en', {
    monthsShort: {
        format:
            'jangfg_febgfg_margfg_aprgfg_maygfg_junegfg_julygfg_auggfg_septgfg_octgfg_novgfg_decgfg'.split('_'),
        standalone:
            'jangfg_febgfg_margfg_aprgfg_maygfg_junegfg_julygfg_auggfg_septgfg_octgfg_novgfg_decgfg'.split('_')
    }
});
  
let gfg = localeData.monthsShort();
console.log("The modified Month Abbreviation array:", gfg);


Output:

The modified Month Abbreviation array: [
     'jangfg',  'febgfg',
     'margfg',  'aprgfg',
     'maygfg',  'junegfg',
     'julygfg', 'auggfg',
     'septgfg', 'octgfg',
     'novgfg',  'decgfg'
]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads