Listings 49-01 through 49-03. I forgot to take into account the smarter way that browsers and internal PC clocks treat dates with respect to the local Daylight Saving Time settings (for many parts of the world). As a result, all three calendar examples show October to have 32 days. That's because the function that calculates the number of days in a month uses the number of days between the first of the current month and the first of the next month. The math -- dividing the two dates in milliseconds by the number of milliseconds in a day -- causes this error because of the "extra hour" that occurs during the last Sunday of October (in many parts of the Northern hemisphere).
One fix prevents calculations from using the absolute borderline case for calculating the number of days in a month. Change the getMonthLen() function as follows:
// number of days in the month
function getMonthLen(theYear, theMonth) {
var oneHour = 1000 * 60 * 60
var oneDay = oneHour * 24
var thisMonth = new Date(theYear, theMonth, 1)
var nextMonth = new Date(theYear, theMonth + 1, 1)
var len = Math.ceil((nextMonth.getTime() -
thisMonth.getTime() - oneHour)/oneDay)
return len
}
October now has 31 days, and the rest of the months continue to have the correct number of days.
Here are the live "Month at a Glance" example files:
Listing 49-01 (all browsers)
Listing 49-02 (all browsers)
Listing 49-03 (IE4+/Windows only and NN6; Do not open with IE5/Mac or Opera 5 because they do not support DOM table methods)