How to get only the current month number using JavaScript?

August 19, 2021 - 2 min read

To get only the current month number, we can use the getMonth() method from the global Date object in JavaScript.

TL;DR

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current month number
const monthNumber = currentDate.getMonth();

console.log(monthNumber); // eg: 8

First, let's make an object (or instance) from the global Date constructor using the new keyword like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();

Now we can use the getMonth() method from the currentDate object to get the month number like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current month number
const monthNumber = currentDate.getMonth();

console.log(monthNumber); // eg: 8
  • IMPORTANT: Since the month number starts from 0, you may see the above output as an error, but it is correct. For example, for September one would expect an output of number 9 but the method gives an output of 8. This is done to keep it easy when working arrays and other native data structures in JavaScript. So keep this little thing in mind while working on this specific method 🤓.

See the above code live in JSBin.

That's all 😃!

Feel free to share if you found this useful 😃.