Open In App

Moment.js Parsing Array

Last Updated : 18 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can pass an array to the moment() function to parse a date represented in the form of an array.

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

Syntax:

moment(Number[])

Parameters: It takes a date described in the form of a Number array. The elements in the array should be in this order [year, month, day, hour, minute, second, millisecond].

Return Value: It returns a Moment object.

Example 1:

Javascript




import moment from 'moment';
  
let arr = [2001, 1, 21];
let date = moment(arr);
console.log(date.format("dddd, Do MMM YYYY, h:mm:ss A"));


Output:

 

Example 2:

Javascript




import moment from 'moment';
  
let arr = [2022, 1, 21,22,31,8];
let date = moment(arr);
console.log(date.format("DD/MM/YYYY-H:mm:ss"));


Output:

 

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads