What is moment.daysInMonth() in Moment.js?

Overview

The daysInMonth() method of the moment library is used to get the number of days in the current month of a time specified.

Syntax

time.daysInMonth()
syntax for daysInMonth() method in moment.js

Parameters

  • time: This the time, a particular date, that we want to get the number of days in its current month.

Return value

The value returned is an integer that represents the number of days in the current month of the time specified.

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 days in current moment
console.log(time1.daysInMonth())
console.log(time2.daysInMonth())
console.log(time3.daysInMonth())
console.log(time4.daysInMonth())
console.log(time5.daysInMonth())
console.log(time6.daysInMonth())
console.log(time7.daysInMonth())

Explanation

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

Free Resources