Get The Current Month In JavaScript

To get the current month in JavaScript –

  1. Create a new date using the Date() constructor.
  2. Use the getMonth() + 1 method to get the month (remembering that January is 0).
  3. You can even use Moment.js methods to get current month.

Let’s first have a look at what all of these methods do.

getMonth(): Returns the month (from 0-11) for the specified date according to local time.

Date(): Creates a JavaScript Date object for today’s date or a date that you specify.

toLocaleString(): Returns a string representing the specified Date object according to language-specific conventions.

Now let’s see how to get the current month in JavaScript using all of these methods.

how to get current month in javascript

How To Get The Current Month In JavaScript?

Example

<script>

var d = new Date();

var n = d.getMonth() + 1;

console.log("Current month is " + n);

</script>

Output: Current month is 5

As we can see from the output, the getMonth() method returns the month from 0-11, so we have added 1 to it to get the current month.

You can also get the name of the current month by using the toLocaleString() method as follows.

Example

<script>

var d = new Date();

var n = d.toLocaleString('default', { month: 'long' });

console.log("Current month is " + n);

</script>

Output: Current month is May

This method returns the name of the month according to the local language conventions.

You can also use thetoLocaleDateString() method to get other locale specific names of the month. For example, for en-US, the months are returned as full names, whereas for other locales like fr-FR, the months are returned as abbreviatednames.

Example

<script>

var d = new Date();

var n = d.toLocaleDateString('en-US', { month: 'long' });

console.log("Current month is " + n);

</script>

Output: Current month is May

As we can see from the above output, for the en-US locale, the month name is returned as a full name.

Similarly, for the fr-FR locale, the month name is returned as an abbreviated name.

Example

<script>

var d = new Date();

var n = d.toLocaleDateString('fr-FR', { month: 'long' });

console.log("Current month is " + n);

</script>

Output: Current month is mai

Instead of long, you can pass short or narrow as an argument to get different formats of the month name.

Example

<script>

var d = new Date();

var n = d.toLocaleDateString('en-US', { month: 'short' });

console.log("Current month is " + n);

</script>

Output: Current month is May

As we can see from the output, for the en-US locale, the month name is returned as an abbreviated name. For October, we would get Oct as the month name if we use short option.

Here’s how to pass narrow as an argument.

Example

<script>

var d = new Date();

var n = d.toLocaleDateString('en-US', { month: 'narrow' });

console.log("Current month is " + n);

</script>

Output: Current month is M

This method is not supported by all browsers, so it’s always a good idea to check the browser compatibility before using it in your projects.

How To Get Current Month In JavaScript Using Moment.js?

Example

<script>

var d = moment().format('MMMM');

console.log("Current month is " + d);

</script>

Output: Current month is May

As we can see from the output, the moment().format(‘MMMM’) method returns the name of the month as a full name.

If you want to get the abbreviated name of the month, you can use the moment().format(‘MMM’) method as follows.

Example

<script>

var d = moment().format('MMM');

console.log("Current month is " + d);

</script>

Output: Current month is May

You can also use the moment().month() method to get the current month. This method returns the month from 0-11, so you have to add 1 to it to get the actual month number.

Example

<script>

var d = moment().month() + 1;

console.log("Current month is " + d);

</script>

Output: Current month is 5

If you want to get the full name of the month in a different language, you can use the moment.localeData().months() method.

Example

<script>

moment.localeData('de');

var d = moment().format('MMMM');

console.log("Current month is " + d);

</script>

Output: Current month is Mai

As we can see from the output, the moment().format(‘MMMM’) method returns the name of the month in German. You can use any language code as an argument to moment.localeData().months() method.

Conclusion

In this tutorial, you have learned how to get the current month in JavaScript using different methods. You can use any of these methods according to your requirements.

If you have any questions, please write them in the comments section below.

Leave a Reply