Open In App

Moment.js Customize Long Date Formats

Improve
Improve
Like Article
Like
Save
Share
Report

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",
        LTS: "h:mm:ss A",
        L: "MM/DD/YYYY",
        l: "M/D/YYYY",
        LL: "MMMM Do YYYY",
        ll: "MMM D YYYY",
        LLL: "MMMM Do YYYY LT",
        lll: "MMM D YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT",
        llll: "ddd, MMM D YYYY LT"
    }
});

After customizing the long-date formats using the above syntax, you can use those formats in the moment().format() utility method.

const moment = require('moment')
moment().format(longDateFormat: String)

Parameters: moment().format() takes a string parameter representing the format in which you want to format your date.

Return value: It returns the date (in the string) in the format which was passed as the argument

Example 1:

Javascript




const moment = require('moment');
  
moment.updateLocale('en', {
    longDateFormat: {
        LT: "h:mm A",
        LTS: "h:mm:ss A",
        L: "MM/DD",
        l: "M/D/YYYY",
        LL: "MMMM Do YYYY",
        ll: "MMM D YYYY",
        LLL: "MMMM Do YYYY LT",
        lll: "MMM D YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT",
        llll: "ddd, MMM D YYYY LT"
    }
});
  
console.log(moment().format('L'))


Output:

 

Example 2:

Javascript




const moment = require('moment');
  
moment.updateLocale('en', {
    longDateFormat: {
        LT: "h:mm A",
        LTS: "h:mm:ss A",
        L: "MM/DD h:mm",
        l: "M/D/YYYY",
        LL: "MMMM Do YYYY",
        ll: "MMM D YYYY",
        LLL: "MMMM Do YYYY LT",
        lll: "MMM D YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT",
        llll: "ddd, MMM D YYYY LT"
    }
});
  
console.log(moment().format('L'))


Output:

 

Reference: https://momentjs.com/docs/#/customization/long-date-formats/



Last Updated : 07 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads