Moment.js Customize Long Date Formats
Last Updated :
07 Sep, 2022
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/
Similar Reads
Moment.js Customize Eras 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 ye
3 min read
Moment.js Customize Invalid Date The Locale#invalidDate in Moment.js: The invalidate property should be a string, a replacement for the "Invalid Date" message you want to customize. Before proceeding, please install the moment.js library using the following command. Installation: npm install moment Syntax: const moment = require('m
2 min read
Moment.js Customize Calendar Format In this article, we will discuss the moment.js Customize Calendar Format in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations. The moment().calendar() function is used to display calendar time which is relative to a giv
3 min read
Node.js Date.format() API The date-and-time.Date.format() is a minimalist collection of functions for manipulating JS date and time module which is used to format the date according to a certain pattern. Required Module: Install the module by npm or used it locally.By using npm.npm install date-and-time --saveBy using CDN li
2 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
1 min read