How to Display the Current Month by Name in JavaScript

Current Month by Name in JavaScript

This is how you can get and display the current month by name using the JavaScript toLocaleDateString method in your web projects.

JavaScript provides a couple of ways to print out the current month by name. We shall show you two ways.

Option 1: Display Current Month by Name From Array

Here we first create an array with all the month names, then we will use the index as a key to finding the month we want.

Create an array with all the month names, and so use your index as a key to finding the current month as shown here.

Next we can use the JavaScript Date() object to retrieve the current month then use the result as the index of the array as follows.

The code above outputs the current month. The method above is not very portable and is error-prone as it relies heavily on what you put into the array.

Option 2: Using the Built-In toLocaleDateString Method

The JavaScript toLocaleDateString method can be used to show parts of a date in your preferred format. It returns a string with a language-sensitive representation of the date portion of this date.

SUGGESTED  Angular 10.0 Release now Available for Download

The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation-dependent.

To display a specific month based on a different date you can replace the above and use the UTC() method like so:

Ref: [ 1 ]

SUGGESTED  Here's How to Redirect Your Old Website to a New One