How to get the Current Month in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 24 20212 min read

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.
index.js
// ๐Ÿ‘‡๏ธ 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.
index.js
// ๐Ÿ‘‡๏ธ 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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee