Open In App

Moment.js Customize Weekday Abbreviations

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the customized weekday abbreviations in detail with examples in Moment.js.

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

Syntax:

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

Parameters:

  • weekdaysShort: The weeksShort 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 is the name of the weekdays in the short form of each weekday and gets at the end as the output.

Javascript




// Importing moment module
const moment = require('moment');
 
const localeData = moment.updateLocale('en', {
    weekdaysShort:
        ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
});
var g = localeData.weekdaysShort();
console.log("Weekdays Array id here:", g)


Output:

Weekdays Array id here: [
  'Sun', 'Mon',
  'Tue', 'Wed',
  'Thu', 'Fri',
  'Sat'
]

Example 2: In this example, we have customized the weekday’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 weekday name and, in the end, getting the current month’s name in JavaScript.

Javascript




// Importing moment module
const moment = require('moment');
 
const localeData = moment.updateLocale('en', {
    weekdaysShortarr: [
        "Sun_GFG", "Mon_GFG", "Tue_GFG", "Wed_GFG",
        "Thu_GFG", "Fri_GFG", "Sat_GFG"],
    weekdaysShort: function (momentToFormat, format) {
        return this._weekdaysShortarr[momentToFormat.day()];
    }
});
 
console.log(
    "Current Weekday is :", localeData.weekdaysShort(moment())
);


Output:

Current Weekday is : Mon_GFG


Last Updated : 30 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads