Open In App

Moment.js Customize Relative Time

Last Updated : 30 Mar, 2023
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 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 time. 

Syntax:

moment.updateLocale('en', {
    relativeTime : Object
});

Parameter:

  • relativeTime: The relative time that has to be set for the Moment object. It is an optional parameter.

Return: It will return the output as a string.

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 for obtaining the relative time in seconds with the string passed as a parameter to the relative time and then return the string with the relative time.

Javascript




// Importing moment module
const moment = require('moment');
let localeData = moment.updateLocale('en', {
    relativeTime: {
        future: "in %s",
        past: "%s ago",
        s: 'Welcome to GeeksForGeeks, a few seconds',
        ss: '%d seconds',
        m: "a minute",
        mm: "%d minutes",
        h: "an hour",
        hh: "%d hours",
        d: "a day",
        dd: "%d days",
        M: "a month",
        MM: "%d months",
        y: "a year",
        yy: "%d years"
    }
});
let a = moment().fromNow();
console.log("The relative time is :", a);


Output:

The relative time is : Welcome to GeeksForGeeks, a few seconds ago

Example 2: In this example, we are using the moment.updateLocale() function for obtaining the relative time in seconds and minutes along with it were using the fromNow(0 functions to get the relative time compared from now and then returning the relative time.

Javascript




// Importing moment module
const moment = require('moment');
const m1 = moment().subtract(10, 'm');
const m2 = moment().subtract(25, 's');
console.log(m1.fromNow());
console.log(m2.fromNow());
moment.updateLocale('en', {
    relativeTime: {
        future: "in %s",
        past: "%s ago",
        s: function (number, withoutSuffix, key, isFuture) {
            return '00:' + (number < 10 ? '0' : '')
                + number + ' minutes';
        },
        m: "01:00 minutes",
        mm: function (number, withoutSuffix, key, isFuture) {
            return (number < 10 ? '0' : '')
                + number + ':00' + ' minutes';
        },
        h: "an hour",
        hh: "%d hours",
        d: "a day",
        dd: "%d days",
        M: "a month",
        MM: "%d months",
        y: "a year",
        yy: "%d years"
    }
});
console.log(m1.fromNow());
console.log(m2.fromNow());


Output:

10 minutes ago
a few seconds ago
10:00 minutes ago
00:25 minutes ago

Reference: https://momentjs.com/docs/#/customization/relative-time/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads