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 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/