Moment.js Parsing Object

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 objects.

The moment() function is used to create a moment using an object.

Syntax:

moment({unit: value, ...})

Parameters: It takes an object representing a specific date. The fields in the object should be used to describe the specifics of the date like year, month, day, hour, minutes, seconds, and milliseconds.

Return Value: It returns a Moment object.

Example 1:

Javascript




import moment from 'moment';
  
let obj = { year: 2022, hour: 12, minute: 55 };
let date = moment(obj);
console.log(date.format("DD/MM/YYYY-H:mm:ss"));


Output:

 

Example 2:

Javascript




import moment from 'moment';
  
let obj = { year: 1999, month: 4, day: 21, minutes: 10, second: 7, milliseconds: 55 };
let date = moment(obj);
console.log(date.format("dddd, Do MMM YYYY, h:mm:ss A"));


Output:

 

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


Contact Us