Open In App

Moment.js Parsing String

Last Updated : 27 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 takes a string representing a specific date

Returns:  A Moment object

Example 1: In this example, we will demonstrate the use of moment():

JavaScript
import moment from 'moment';

let day = "2001-01-21"
let date = moment(day);
console.log(date.format("dddd, Do MMM YYYY, h:mm:ss A"));

Output:

 

Example 2:

JavaScript
import moment from 'moment';

let day = "2010-02-28 13:40:55"
let date = moment(day);
console.log(date.format("DD/MM/YYYY-H:mm:ss"));

Output:

 

Reference: https://momentjs.com/docs/#/parsing/string/


Next Article

Similar Reads