Moment.js Parsing String + Formats
Last Updated :
28 Apr, 2025
Moment.js Parsing String + Formats is used when we are not sure of the exact format of an input date string. However, we can specify the possible formats in an array as a parameter for it to try and match. It is the same as String + Format, with the exception that this supports multiple formats.
As it takes more time for parsing each of the formats, it is recommended to use a lesser number of formats or use a single format.
Syntax:
moment(String, String[], String, Boolean);
Parameters: This method accepts four parameters.
- String: This is the date string to be parsed.
- String[]: It is the array of strings that specifies all the formats that have to be parsed.
- String: It is the locale to be parsed by the method.
- Boolean: It specifies whether strict parsing would be used for the parsing.
Return Value: This function returns the parsed Moment object.
Formatting Preference:
- It prefers valid dates over invalid ones.
- It prefers formats earlier in the array than later.
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: Here, the input will be matched with the format "MM-DD-YYYY".
JavaScript
const moment = require("moment");
// Match the date with the given formats
let date = moment(moment(
"11-29-2022",
["MM-DD-YYYY", "DD-MM-YYYY"])
);
console.log("The date is", date);
Output:
The date is Moment<2022-11-29T00:00:00+05:30>
Example 2: Here, the input will be matched with the format "YYYY-MM-DD".
JavaScript
const moment = require("moment");
// Match the date with the given formats
let date = moment(moment(
"2022-10-25",
["MM-DD-YYYY", "YYYY-MM-DD"])
);
console.log("The date is", date);
Output:
The date is Moment<2022-10-25T00:00:00+05:30>
References: https://momentjs.com/docs/#/parsing/string-formats/
Similar Reads
Moment.js Parsing String + Format Moment.js Parsing String+Format is used when we want to parse a date string through the given format string. The parser will ignore the non-alphanumeric characters of the format. It returns the parsed date as a Moment object. Syntax: moment(String, String, Boolean); Parameters: This method accepts t
1 min read
Moment.js Parsing String Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with a string to parse dates represented as strings. The moment() function is used to create a moment using a string. Syntax: moment(String) Parameters:Â It take
1 min read
Moment.js Parsing Special Formats Moment.js is a JavaScript date library for parsing, validating, manipulating, and formatting dates. In this article, we will learn about the parsing of Special Formats in Moment.js. The ISO-8601 format is used as a standard for parsing the date and time. We can also specify HTML5 constants that can
2 min read
Moment.js Parsing Defaults Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function by providing only a few parameters and the rest will default to the current day, month, or year and 0 for an hour, minute, second, and millisecond. The moment() functio
1 min read
Moment.js Parsing Date Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with an object to parse dates represented as Date object. The moment() function is used to create a moment using a Date object. Syntax: moment( Date ) Parameters
1 min read