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

Macs are one day in the future

A bug in Netscape Navigator 2.0x on the Macintosh platform causes instance of the Date object representing the current date to be approximately one day in the future. Since the number of people using this browser is small, you might want to ignore it (we prefer to do that). However, if you are still concerned, you can use the following function to solve the problem:

 function fixDate(date) {
   var base = new Date (0)
   var skew = base.getTime()
   if (skew > 0)
   date.setTime(date.getTime() - skew)
}

You then pass all instances to this function in the following fashion:

 var now = new Date()
 fixDate(now)

Date Numeric Conventions

Dates in JavaScript, as acquired from Java, use integers to specify values that succeed each other. For example, the first day of a month is 1, the second is 2, the third is 3, and so on. The numbers used are not always so obvious. Most date attributes are actually zero-based; that is, they start at zero. For example, the first minute of an hour is 0, the second one is 1, the third minute is 2, and so on. This behavior is clearly the result of array structure influence. In all modern programming languages, including Perl, C++, and also Java, the first element of an array is normally the one which has a subscript, or index, of 0. JavaScript, which was born of Java, also implemented this rule, as you have seen in the previous chapter. The following table summarizes the numeric conventions of each attribute of a Date instance:

Table 14-1 Ranges of date attributes.


Date attribute Range

seconds 0 - 59
minutes 0 - 59
hours 0 - 23
day 0 - 6
date 1 - 31
month 0 - 11
year number of years since 1900 (e.g., 97)

Most importantly, when referring to client-side JavaScript, the date and time refer to the client side. All values are the ones passed to the script by the browser. As you might know, especially if you are a Mac or Windows environment programmer, all applications on your computer have access to the machine clock, including the current time and date. If the system clock is not set to the current time and date, JavaScript will use these incorrect values in the script, possibly surprising the user. However, unlike computers from about ten years ago, most computers today come with the correct local time, enabling the user to be worry-free regarding the current time setting. You should assume, then, that the clock is set to the correct local settings.

Date Method Categories

JavaScript provides JavaScript scripters with a handful of methods to deal with instances of the Date object. As you have seen, Date instances are not very useful at their top level. However, extracting and manipulating their data makes them one of the most important elements of the language.

The whole bulk of methods may seem dazzling if you approach them at once. Therefore, we have chosen to divide them into four groups, according to their operation:

  • get methods
  • set methods
  • to methods
  • parse methods

The first type of method is by far the most useful one. get methods return an integer corresponding to the attribute of the instance you desire. You can “get” the year number, the month number, the hour one, and so on. set methods enable you to modify the value of a certain attribute of an existing instance. These methods accept integer values rather than returning them. You actually “set” the values of attributes with these statements. to methods convert the date into a string according to arguments passed over to the method. You can then take advantage of the string format with string methods and properties, such as the method split(). parse methods simply interpret date strings.

get Methods

getYear()

The getYear() method returns the current year stored in an instance of the Date object type. Years are represented in integers, specifying the year of the 20th century. For example, this book was written in 97, not 1997. If you ask the user to enter a certain year, you must specify that the two-digit format is necessary, or validate the input instead. The latter is demonstrated in the following script segment:

var now = new Date()
var year = now.getYear()
while (1) {
  var guessYear = parseInt(prompt("Enter current year:", ""))
  if (guessYear > 1900)
 guessYear –= 1900
  if (guessYear == year) {
 alert("That's right!")
 break
  } else
 alert("Wrong answer! Try again...")
}

The current year (based on the system clock) is extracted from the instance now, created according to default arguments (current time and date). A loop without a terminating condition is executed next. The user is asked to enter the current year, according to his or her knowledge! If the user enters the year using the four-digit convention, it is reduced to the two-digit one, via an if statement. Another if statement checks if the user entered the correct year. If so, the proper message is displayed, and the loop is broken up with a break statement. Otherwise, a message informs the user that his or her input was incorrect, and the loop iterates once more. You will often find the first if statement in the form of a conditional operation:

guessYear = (guessYear > 1900) ? guessYear – 1900 : guessYear

The if statement is undoubtedly better in this situation because it is clearer. As a matter of fact, it is also simpler and shorter (count the number of characters in each!). When the year 2000 arrives, you will probably need to find a workaround, or more likely, you won’t be programming in JavaScript.

getMonth()

The getMonth() method extracts and returns the month of its calling object. Months range from January to December, or more accurately, from 0 to 11. Here is a simple example demonstrating this method as well as an array instance:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us