Open In App

Moment.js Customize Ordinal Names

Last Updated : 14 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. The ordinals of date values of the Moment output is customizable according to our needs. We will be using the moment.updateLocale() method for this.

Syntax:

moment.updateLocale('en', {
    ordinal: callback_function
});

Parameters: The updateLocale method will accept an object that has the ordinal property that can be used to specify the callback function used for customizing the ordinal names.

 

The default ordinals of the numbers are as follows:

  • 1: st
  • 2: nd
  • 3: rd
  • 4: th
  • 5: th
  • 6: th
  • 7: th
  • 8: th
  • 9: th

Note: This will not work in the normal Node.js program because it requires the moment.js library to be installed.

Moment.js can be installed using the following command:

npm install moment

Example 1:

Javascript




// Acquiring the plugin
var moment = require("moment");
  
// Customizing the ordinal method
var ordinal_name = moment.updateLocale("en", {
    ordinal: function (num) {
        var last_digit = num % 10;
        var ordinal = ~~((num % 100) / 10) === 1
            ? "`th"
            : last_digit === 1
                ? "`st"
                : last_digit === 2
                    ? "`nd"
                    : last_digit === 3
                        ? "`rd"
                        : "`th";
        return num + ordinal;
    },
});
  
console.log("Ordinal name of 3 is:", ordinal_name.ordinal(3));
console.log("Todays Date is:", moment().format('Do'))


Output:

Ordinal name of 3 is: 3`rd
Todays Date is: 13`th

Example 2:

Javascript




// Acquiring the plugin
var moment = require("moment");
  
// Customizing the ordinal method
var ordinal_name = moment.updateLocale("en", {
    ordinal: function (num) {
        var last_digit = num % 10;
        var ordinal = ~~((num % 100) / 10) === 1
            ? "-TH"
            : last_digit === 1
                ? "-ST"
                : last_digit === 2
                    ? "-ND"
                    : last_digit === 3
                        ? "-RD"
                        : "-TH";
        return num + ordinal;
    },
});
  
console.log("Ordinal name of 5 is:", ordinal_name.ordinal(5));
console.log("Todays Date is:", moment().format('Do'))


Output:

Ordinal name of 5 is: 5-TH
Todays Date is: 13-TH

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



Similar Reads

Moment.js Customize Month Names
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 t
2 min read
Moment.js Customize Calendar Names
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
3 min read
Moment.js moment().max(Moment|String|Number|Date|Array) Method
The moment().max() method limits the moment to a maximum of another moment value. We can also say that m1.max(m2) is the same as m1 = moment.min(m1, m2). This method is the counterpart of moment.min() method. Syntax: moment().max(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js moment().min(Moment|String|Number|Date|Array) Method
The moment().min() method limits the moment to a minimum of another moment value. We can also say that m1.min(m2) is the same as m1 = moment.max(m1, m2). This method is the counterpart of moment.max() method. Syntax: moment().min(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js Customize Relative Time Thresholds
This article will discuss the moment.js Customize Relative Time Thresholds in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. The moment.relativeTimeThreshold() is used in duration. Humanize the display such that the duration is shown as a few seconds, a minute, an
3 min read
Moment.js Customize Relative Time
Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. In this article, we will discuss the moment.js customize relative time in detail with examples. The moment.updateLocale() function allows us to help in obtaining the relative time. It helps us to fulfill the need for processing the relative
3 min read
Moment.js Customize AM/PM
In this article, we will learn how to customize the AM/PM logic in Moment.js. The meridiem callback function can be used for customizing the meridiem as per locale. It returns the AM/PM string based on the logic of the callback function. Syntax: moment.updateLocale('en', { meridiem: Function }); The below example will help to demonstrate the custom
1 min read
Moment.js Customize Changing Time Source
In this article, we will learn how to customize the moment.js time source as per one's requirements. Customizing/Changing the time source of moment.js: To customize or change the time that moment.js gives, use can assign moment.now() to a custom function that returns the number of milliseconds since the Unix epoch, i.e, January 1, 1970. Syntax: mom
1 min read
Moment.js Customize Long Date Formats
Locale longDateFormat in Moment.js: The longDateFormat should be passed an object for each long date format, i.e. `L LL LLL LLLL LT LTS`, you want to customize. Before proceeding, please install the moment.js library using the following command. Installation: npm install moment Syntax: moment.updateLocale('en', { longDateFormat : { LT: "h:mm A", LT
2 min read
Moment.js Customize Weekday Abbreviations
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
2 min read