Borislav Hadzhiev
Sun Oct 24 2021·2 min read
Photo by Scott Goodwill
To get the current month:
new Date()
constructor to get a date object.getMonth()
method on the object and add 1
to the result.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.
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:
default
, it can vary based on the user's browser preferences.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.