Moment.js moment().daysInMonth() Function
Last Updated :
20 Mar, 2023
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
const moment = require( 'moment' );
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
const moment = require( 'moment' );
function getNoOfDays(date) {
return moment(date, "YYYY-MM" ).daysInMonth()
}
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
Share your thoughts in the comments
Please Login to comment...