Skip to content
Related Articles

Related Articles

Moment.js Parsing Defaults

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Jul, 2022
View Discussion
Improve Article
Save Article

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() function supplied with only a few parameters can be used to get the default values for the day, month, year, hour, minute, second, and millisecond.

Syntax:

moment(string1,string2) | moment(obj)

Parameters: It takes in two strings, the first one specifying the date and the second specifying the format of the date. Apart from the strings, it may also take in an object specifying the date.

Returns: A Moment object.

Example 1:

Javascript




import moment from 'moment';
  
let date = moment('02/2001/21', 'MM/YYYY/DD');
console.log(date.format('DD-MM-YYYY hh:mm:ss A'));

Output:

 

Example 2:

Javascript




import moment from 'moment';
  
let date = moment({year: 1999, hour: 14, minute: 45});
console.log(date.format('dddd, Do MMM YYYY, h:mm:ss A'));

Output:

 

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!