Open In App

Moment.js Customize Minimal Weekday Abbreviations

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

The moment.updateLocale() function allows us to add minimal weekday abbreviations to the locale customization. It helps us to fulfill the need for more processing to calculate the Minimal Weekday Abbreviations, which are shorthand and customizable according to the user’s need.

Syntax:

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

Parameters:

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

Returns: This method returns the current weekday 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 with the name of the minimal weekday abbreviations of two letters in the short form of each weekday and getting output at the end.

Javascript




// Importing moment module
const moment = require('moment');
let localeData = moment.updateLocale('en',
    {
        weekdaysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
    });
let a = localeData.weekdaysMin();
console.log("The array of all Minimal Weekday Abbreviations stored", a);


Output:

The array of all minimal weekday abbreviations stored
[
  'Su', 'Mo',
  'Tu', 'We',
  'Th', 'Fr',
  'Sa'
]

Example 2: In this example, we are simply using the updatelocale() function with the name of the minimal weekday abbreviations of two letters with “gfg” at the end of each in the short form of each weekday and getting at the end of the current weekday as the output.

Javascript




// Importing moment module
const moment = require('moment');
let localeData = moment.updateLocale('en', {
    weekdaysMinarr: [
        "Su_gfg", "Mo_gfg", "Tu_gfg", "We_gfg", "Th_gfg", "Fr_gfg", "Sa_gfg"],
    weekdaysMin: function (momentToFormat, format) {
        return this._weekdaysMinarr[momentToFormat.day()];
    }
});
let a = localeData.weekdaysMin(moment());
console.log("The current Weekday of Minimal Weekday Abbreviations is:", a);


Output:

The current Weekday of Minimal Weekday Abbreviations is: We_gfg

Reference: https://momentjs.com/docs/#/customization/weekday-min/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads