Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Let’s follow the script step by step, explaining the task of every function.

getTime()

This function simply returns a string representing the current local time in the following format:

hours: minutes AM/PM

Note that there are no spaces between any characters. The function is based on the same algorithm as the first part of setClock() in Example 14-1. Refer to the explanation regarding that example for further insights.

leapYear(year)

This function returns true if the current year is a leap year. Otherwise it returns false. The basic rule used for the decision is that a leap year occurs every four years, in the same year of the summer Olympic games. More exactly, if the year is divisible by 4, it is a leap year. Therefore, the modulo operator suits the case perfectly. If year % 4 is zero, the year is divisible by 4, meaning the current year is a leap year. Otherwise, the year is not divisible by 4, so false is returned. An obvious call to this function is:

if (leapYear(current year))
   // is a leap year
else
  // is not a leap year

Another possibility is to use the returned value in a conditional statement, or operation (?:).

Note that the parameter of the function must accept an integer value, which is reasonable when computing years.

getDays(month, year)

This function accepts two arguments: a month and a year. An array of 12 elements is then created. The array is an instance of the built-in Array object. Therefore, the keyword new is used. Each element of the array represents the number of days in its corresponding month. ar[0] holds the number of days in January (31); ar[11] holds the number of days in December. The array is simply assigned the proper data, according to the constant number of days in each month. However, the number of days in February is not constant. In leap years there are 29 days in February, whereas in all other years there are 28 days. The function leapYear() is used to decide if the specified year is a leap one. This situation is a typical one for a conditional operator, because one of two values is to be assigned to a variable depending on the value of the condition (the Boolean value returned by the function leapYear()). Notice the extensive use of comments. They help you understand the script.

The value returned by the function is equal to the number of days in the month passed over to the function upon calling. For example, if the value of month is 0 (as passed to the function), the value ar[0] == 31 is returned by the function.

Note that both arguments must be integers. The month must be specified as an integer between 0 and 11, 0 representing January, and 11 representing December.

getMonthName(month)

This function accepts the integer value of a certain month (0 - January, 11 - December) and returns the full name of the function, obviously in the form of a string. This function, like its preceding one, uses an instance of the Array object to store constant values. The name of the desired month is retrieved from the array by its index (subscript).

setCal()

At first, the function creates a new instance of the Date object, holding the attributes of the current local time. The current year is assigned to year via the method getYear(), and the current month is assigned to month via the method getMonth(). The name of the month, returned by getMonthName(), is assigned to monthName. After the current date is assigned to date, the instance now is assigned null, a good JavaScript programming practice.

The following statement of the function is:

var firstDayInstance = new Date(year, month, 1)

It creates a new instance of the Date object; this time it is for the first day of the current month. Therefore, the value 1 is used to specify the date. This obviously influences the day of the week on which the date occurred. This value is assigned to firstDay in the following statement. The instance firstDayInstance is then assigned null. This script segment computes the day of the week (Sunday, Monday, Tuesday, etc.) on which the month started. Another possible way to achieve this goal is to create an instance of the current date as usual:

var firstDayInstance = new Date() // not first day yet!

You then need to set the date to 1, via the setDate() method. You should use the following statement to do so:

firstDayInstance.setDate(1)

The next portion of the function consists of only one statement. It assigns days the number of days in the current month.

The last statement of the function draws the calendar. Here it is:

drawCal(firstDay + 1, days, date, monthName, 1900 + year)

The arguments are:

  1. Integer value of the first day of the month + 1. That is, 1 for Sunday, 2 for Monday, 3 for Tuesday.
  2. The number of days in the specified month.
  3. The specified date.
  4. The name of the specified month (e.g. &,#147;January,” “February,” “March”).
  5. The specified year, as a four-digit integer (e.g., 1997, 1998).

drawCal(firstDay, lastDate, date, monthName, year)

This function’s main task is to print the calendar table. Before it does so, the HTML structure of the table must be constructed.

The first part of the function assigns values to attributes associated with the final format of the table. Such attributes are the size of cells, font colors, and more. Here is the full list, including the variable names and their roles:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us