Open In App

Moment.js moment().month() Method

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The method `moment().month()` in Moment.js is employed to retrieve or modify the month of the Moment object. It’s important to note that the months in Moment.js are zero-indexed. Consequently, the valid range for months is 0 to 11, where 0 corresponds to January and 11 to December. If a value greater than 11 is specified, it will roll over to the subsequent year.

The month can also be set using a string of the month’s full name or shorthand.

Syntax:

moment().month( Number|String );

Parameters: This method accepts a single parameter as mentioned above and described below:

  • Number|String: It is the month that has to be set for the Moment object. it is an optional parameter.

Return Value: This method returns the current month of the Moment.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

Example 1: The below examples will demonstrate the Moment.js moment().month() Method.

Javascript




const moment = require('moment');
 
console.log("Current Date:", moment().toString())
console.log("Current month is:", moment().month())
 
let month10 = moment().month(10);
console.log(
    "Moment with Month of 10 is:",
    month10.toString()
)
 
let month24 = moment().month(24);
console.log(
    "Moment with Month of 24 is:",
    month24.toString()
)


Output:

Current Date: Wed Jul 13 2022 01:30:32 GMT+0530
Current month is: 6
Moment with Month of 10 is: Sun Nov 13 2022 01:30:32 GMT+0530
Moment with Month of 24 is: Sat Jan 13 2024 01:30:32 GMT+0530

Example 2: The below examples will demonstrate the Moment.js moment().month() Method.

Javascript




const moment = require('moment');
 
console.log("Current Date:", moment().toString())
console.log("Current month is:", moment().month())
 
let monthDecember = moment().month("December");
console.log(
    "Moment with Month of December is:",
    monthDecember.toString()
)
 
let monthFeb = moment().month("Feb");
console.log(
    "Moment with Month of Feb is:",
    monthFeb.toString()
)


Output:

Current Date: Wed Jul 13 2022 01:30:32 GMT+0530
Current month is: 6
Moment with Month of December is: Tue Dec 13 2022 01:30:32 GMT+0530
Moment with Month of Feb is: Sun Feb 13 2022 01:30:32 GMT+0530


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads