Welcome to the second part of an eight-part series on building a web page calendar. In this part of the series, I will explain the function that is used to create the current calendar. When the Show Current Month's Calendar button is clicked, it should call a function that will display the calendar of the current month. We need to modify it; the showCal() function we saw in the previous part has only one line. We will change this function and use it as the showCurrentMonth() function.
Creating a Web Page Calendar - The showCurrentMonth() Function (Page 3 of 4 )
Now that we've summarized the JavaScript Date object, we'll use it for our function.
Array for Month Names
As we saw in the summary earlier, the date object will return the day of the week, date (month number) and month as integers. If we need the words (names), we have to do the conversion ourselves. For this series, the only thing we need to convert is the names of the months. For this we need an array.
The following global scope array is declared:
var monthArray = new Array(12);
monthArray[0] = "January";
monthArray[1] = "February";
monthArray[2] = "March";
monthArray[3] = "April";
monthArray[4] = "May";
monthArray[5] = "June";
monthArray[6] = "July";
monthArray[7] = "August";
monthArray[8] = "September";
monthArray[9] = "October";
monthArray[10] = "November";
monthArray[11] = "December";
The number (0, 2 ... 11) returned by the date object is what I have used as the index for each element of the array. The value of each element in the array is the corresponding name of the month of the year. So, if our date object returns a month value of 5, our code will use this array to know that the name for the month is June.