How to get the Current Month in JavaScript


Borislav Hadzhiev

Last updated: Oct 24, 2021


Photo from Unsplash

Get the Current Month in JavaScript #

To get the current month:

  1. Use the new Date() constructor to get a date object.
  2. Call the getMonth() method on the object and add 1 to the result.
  3. The getMonth method returns a zero-based month index so adding 1 returns the current month.
// 👇️ Get digit of Current Month const currentMonth = new Date().getMonth() + 1; console.log(currentMonth); // 👉️ 10 // 👇️ Get Name of Current Month const nameOfMonth = new Date().toLocaleString( 'default', {month: 'long'} ); console.log(nameOfMonth); // 👉️ October // 👇️ Get Localized Names of Current Month const nameOfMonthUS = new Date().toLocaleString( 'en-US', {month: 'long'} ); console.log(nameOfMonthUS); // 👉️ October const nameOfMonthDE = new Date().toLocaleString( 'de-DE', {month: 'long'} ); console.log(nameOfMonthDE); // 👉️ Oktober

We used the new Date() constructor to get a date object for the current date.

In the first example, we called the getMonth method on the result and added 1 to get the digit representation of the current month.

The getMonth method returns a zero-based index representation of the month, meaning January is 0 and December is 11.

To get an accurate digit representing the current month, we had to add 1 to the result.

In our following examples, we used the toLocaleString method to get the name of the current month.

We passed the following parameters to the method:

  1. the locale - in which language the name of the month should be returned. By specifying default, it can vary based on the user's browser preferences.
  2. the options object - we set the month setting to long to get the full name of the month. Other possible values are short and narrow.
// 👇️ October console.log(new Date().toLocaleString( 'en-US', {month: 'long'}) ); // 👇️ Oct console.log(new Date().toLocaleString( 'en-US', {month: 'short'}) ); // 👇️ O console.log(new Date().toLocaleString( 'en-US', {month: 'narrow'}) );

To view other possible parameters you can pass to the toLocaleString method, check this section from the MDN docs out.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.