Open In App

Moment.js moment().daysInMonth() Function

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().daysInMonth() function is used to get the number of days in the month of a particular month in Node.js. It returns an integer denoting the number of days. 

Syntax:

moment().daysInMonth();

Parameters: This function has no parameters. 

Return Value: This function returns an integer. 

Installation of moment module:

You can visit the link to the Install moment module. You can install this package by using this command.

npm install moment

After installing the moment module, you can check your moment version in the command prompt using the command.

npm version moment

Project Structure:

After that, you can just create a folder and add a file, for example, index.js as shown below.

Example 1: Filename: index.js 

javascript




// Requiring the module
const moment = require('moment');
 
// Function call
let result = moment("2020-02", "YYYY-MM").daysInMonth()
 
console.log("No of days in 2020-02 is:", result)


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

No of days in 2020-02 is: 29

Example 2: Filename: index.js 

javascript




// Requiring the module
const moment = require('moment');
  
function getNoOfDays(date) {
   return moment(date, "YYYY-MM").daysInMonth()
}
  
// Function call
let result = getNoOfDays("2019-03");
console.log("No of days in given date is:", result)


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

No of days in given date is: 31

Reference: https://momentjs.com/docs/#/displaying/days-in-month/


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads