Get the name of the current month

In this example, we use the getMonth() method to get the current month. Note this JavaScript built-in time function returns a numerical value between 0 and 11; where 0 corresponds to January, 1 corresponds to February, 2 corresponds to March, and so on. Because getMonth() returns only a numerical value, we need to somehow associate a name of the month to a numerical value. We do that with an array as the example shows below.

Before we continue, the following shows the output of our script.

Scripting code

Step 1: place the following code in the head section (between <head> and </head>) of your web document.

<script language="javascript">
function CurrentMonthName ()
var dateObj = new Date();
// create an array
var monthsArr = new Array();
// get current month
var currMonth = dateObj.getMonth();
// store month names into our array
monthsArr[0] = "January";
monthsArr[1] = "February";
monthsArr[2] = "March";
monthsArr[3] = "April";
monthsArr[4] = "May";
monthsArr[5] = "June";
monthsArr[6] = "July";
monthsArr[7] = "August";
monthsArr[8] = "September";
monthsArr[9] = "October";
monthsArr[10] = "November";
monthsArr[11] = "December";
return monthsArr[currMonth];

Our CurrentMonthName () function starts with by declaring a date object called dateObj. Next, we create an array called monthsArr. Then, we create the currMonth variable to store the current month (0 - 11). We next start to fill our array with names of the month. Note each array index contains only one month name. Finally, we return the name of the month by accessing the array index of the variable currMonth (corresponding to current month: 0 - 11).

Step 2: Call the CurrentMonthName () function from your web page:

<script language="javascript" type="text/javascript">
document.write ("Current month is " + CurrentMonthName());

To use CurrentMonthName () function we call the function in step 2. The function returns us the name of the month (like January, February, or March, etc).