How to Parse String to Date with MomentJS?
Last Updated :
26 Jul, 2024
Moment.js is the date library in JavaScript that provides parsing, validation, manipulation, and formatting of dates and times. It has several methods to parse strings into date objects, accommodating various formats and time zones.
Run the below command before running the code in your local system:
npm i moment
These are the following approaches we are going to discuss:
Using Default Parsing
In this approach, we are parsing a string to a date using Moment.js's default parsing. By passing the string '2024-07-26' directly to moment(), it interprets the string as a date and returns a Moment object. The format('YYYY-MM-DD') method is then used to display the date in the desired format.
Example: The below example performs Default Parsing to Parse string to date with moment.js.
JavaScript
// script.js
const moment = require('moment');
let date1 = moment('2024-07-26');
console.log(date1.format('YYYY-MM-DD'));
Output:
2024-07-26
In this approach, we are parsing a string to a date using a specific format with Moment.js. By specifying the format 'DD/MM/YYYY' when calling moment(), it correctly interprets the string '26/07/2024' according to that format. The format('DD/MM/YYYY') method is then used to display the date in the same format as the input.
Example: The below example performs Specific Format Parsing to Parse string to date with moment.js.
JavaScript
const moment = require('moment');
let date2 = moment('26/07/2024', 'DD/MM/YYYY');
console.log(date2.format('DD/MM/YYYY'));
Output:
26/07/2024
Using moment.utc() for UTC Parsing
In this approach, we are parsing a string to a date using moment.utc() to handle UTC parsing. By passing the string '2024-07-26T15:00:00Z' to moment.utc(), it interprets the date and time in UTC. The format('YYYY-MM-DD HH:mm:ss') method is then used to display the date and time in the specified format.
Example: The below example uses moment.utc() for UTC Parsing.
JavaScript
const moment = require('moment');
let date3 = moment.utc('2024-07-26T15:00:00Z');
console.log(date3.format('YYYY-MM-DD HH:mm:ss'));
Output:
2024-07-26 15:00:00
Similar Reads
How To Set Time With Date in Moment.js? Moment.js is a powerful JavaScript library that makes it easy to work with dates and times. Sometimes, we want to set a specific time to an already existing date. For example- we have a date like "2024-08-15" and we want to set the time to "9:00 AM". Moment.js provides simple ways to do this. In thi
2 min read
How to Remove Time From Date with Moment.js? Moment.js is a widely used JavaScript library for managing and manipulating dates and times. It simplifies complex date operations and makes it easier to work with dates in a consistent format. One common task is to extract just the date portion from a full timestamp, ignoring the time part. This is
2 min read
How to get Yesterday's Date with Momentjs? MomentJS works on Date and Time Manipulation to provide a convenient way to handle, manipulate, and format dates and times in JavaScript. It simplifies common date operations such as parsing, validating, manipulating, and displaying dates. To get yesterday's date using Moment.js, you can use various
2 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
How to Format Date with Moment.js? Formatting dates is essential for presenting date and time information in a way that's readable and useful for users. Moment.js provides several methods for formatting dates from simple and predefined formats to more complex and localized representations.Below are different approaches:Table of Conte
2 min read