Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is the moment().toArray() method in moment.js?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The moment().toArray() method returns an array containing the year, month, day, hour, minute, seconds, and milliseconds of a particular time or date.

Syntax

time.toArray()
Syntax for toArray()

Parameter

time: This is the time whose parts we want to return as an array.

Return value

An array containing the year, month, day, hour, minute, seconds, and milliseconds of a time or date is returned.

Example

// require the moment module
const moment = require("moment")
// create some dates and time
const time1 = moment([2010, 1, 14, 10, 25, 50, 125])
const time2 = moment(new Date) // current date and time
const time3 = moment(1318781876406)
const time4 = moment({hour: 20, minute: 20, second: 34})
const time5 = moment([2022, 5, 17])
const time6 = moment([2025, 4, 18])
const time7 = moment().add(25, "seconds");
// get the array of all parts of time created
console.log(time1.toArray())
console.log(time2.toArray())
console.log(time3.toArray())
console.log(time4.toArray())
console.log(time5.toArray())
console.log(time6.toArray())
console.log(time7.toArray())
Using the toArray() method in moment.js

Explanation

  • Line 2: We require the moment.js package.
  • Lines 5–11: We create some times and dates.
  • Lines 14–20: We use the toArray() method to get the number of days in the current month of the times we created and print the values to the console.

RELATED TAGS

javascript

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring