Open In App

Moment.js Customize Calendar Names

Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is very easy to customize. In general, you should create a locale setting with your customizations in the node js.

In this article, we will discuss the moment.js Customize Calendar Names in detail with examples.

The moment.updateLocale() function allows us to customize calendar objects for a locale set. It helps us to fulfill the need for more processing of the calendar name. The calendar name 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', {
    calendar : Object
});

Parameters:

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

Returns: This method returns the current calendar 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 calling the moment.updateLocale() function with the customized calendar set as its parameters. We call this same function for the output as the current day and time, yesterday’s day and time, and the day before yesterday in the JavaScript programming language.

Javascript




// Importing moment module
const moment = require('moment');
let localeData = moment.updateLocale('en', {
    calendar: {
        lastDay: '[Yesterday at] LT',
        sameDay: '[Today at] LT',
        nextDay: '[Tomorrow at] LT',
        lastWeek: '[last] dddd [at] LT',
        nextWeek: 'dddd [at] LT',
        sameElse: 'L'
    }
});
let a = moment().calendar();
// subtracting 1 day from now
let b = moment().subtract(1, 'days').calendar();
// subtracting 2 day from now
let c = moment().subtract(2, 'days').calendar();
console.log("The current time and day:", a)
console.log("The current time and day
    subtracted by 1 day: ",b)
console.log("The current time and day
    subtracted by 2 day: ",c)


Output:

The current time and day: Today at 5:46 AM
The current time and day subtracted by 1 day: Yesterday at 5:46 AM
The current time and day subtracted by 2 day: last Tuesday at 5:46 AM

Example 2: In this example, we are simply calling the moment.calendar() function to get the result as the current time along with today as the output by passing the date as the moment as the function’s parameter in the JavaScript programming language.

Javascript




// Importing moment module
const moment = require('moment');
 
// Function call
function getCalendar(date) {
    return moment().calendar();
}
 
// Function call
let result = getCalendar(moment);
console.log("Result:", result);


Output:

Result: Today at 10:02 PM

Reference: https://momentjs.com/docs/#/customization/calendar/



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