Open In App

Moment.js Customize Eras

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

Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. An era can be defined as the time interval with name and year numbering. 

In this article, we will learn how to customize Eras in Moment.js The Notations of the eras are described below:

  • A positive year number is considered to be a part of the era AD. Eg: 2022 is considered as 2022 AD. 
  • A negative year number is considered to be a part of the era BC. Eg: -0250 is considered as 251 BC.

Syntax:

moment.updateLocale('en', {
    eras: {
        since:  String,              
        until:  +Infinity/-Infinity, 
        offset: 1,                   
        name:   'Anno Domini/Before Christ', 
        narrow: 'AD/BC',
        abbr:   'AD/BC'
    },
});

Parameters: This method accepts 6 parameters as mentioned above and described below:

  • since: We enter the date here in the form of a String. It indicates the start of an era.
  • until: We enter either +Infinity or -Infinity here. It indicates the end of the era.
  • offset: We set our desired offset here.
  • name: The name of the era is mentioned here.
  • narrow: We enter the narrow name of an era here.
  • abbr: Abbreviation of the era is entered here.

Return Value: This function returns the customized era as output.

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: We will enter a negative year number here which corresponds to the BC era.

Javascript




// Acquiring the plugin
const moment = require("moment");
 
let era = moment.updateLocale("en", {
    eras:
    {
        until: -Infinity,
        since: "0000-05-16",
        offset: 1,
        name: "Before Christ",
        narrow: "BC",
        abbr: "BC",
    },
});
 
console.log("Name of the era is:", era._eras.name);
console.log("Abbreviation of the era is:", era._eras.abbr);
console.log("This era is since:", era._eras.since);
console.log("It will last until:", era._eras.until);


Output:

Name of the era is: Before Christ
Abbreviation of the era is: BC
This era is since: 0000-05-16
It will last until: -Infinity

Example 2: We will enter a positive year number here which corresponds to the AD era.

Javascript




// Acquiring the plugin
const moment = require("moment");
 
let era = moment.updateLocale("en", {
    eras: {
        since: "0001-01-01",
        until: +Infinity,
        offset: 1,
        name: "Anno Domini",
        narrow: "AD",
        abbr: "AD",
    },
});
 
console.log("Name of the era is:", era._eras.name);
console.log("Abbreviation of the era is:", era._eras.abbr);
console.log("This era is since:", era._eras.since);
console.log("It will last until:", era._eras.until);


Output:

Name of the era is: Anno Domini
Abbreviation of the era is: AD
This era is since: 0001-01-01
It will last until: Infinity

References: https://momentjs.com/docs/#/customization/eras/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads