
| JavaScript does not have a date data type. However, the
date object and its methods enable you to work with dates and times in
your applications. The date object has a large number of methods for
getting, setting, and manipulating dates. It does not have any properties.
JavaScript handles dates very similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds (1000 milliseconds equals 1 second) since January 1, 1970 00:00:00. To create a date object:
where varName is a JavaScript variable name for the date object being created. Since we cannot use the "real" Date Object we use the key new to create a new instance of the Date Object. The Date Object is used as a template to create instances of the Date Object which we can use. The parameters for the Date constructor can be any of the following:
The Date object has a large number of Methods for handling dates and times. The Methods fall into these broad categories:
The "get" and "set" methods enable you to get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay() Method that returns the day of the week, but no corresponding setDay() Method, because the day of the week is set automatically. These methods use integers to represent these values as follows:
For example, suppose you define the following date:
Then Xmas95.getMonth() returns 11, and Xmas95.getYear() returns 95. The getTime and setTime methods are useful for comparing dates. The getTime method returns the number of milliseconds since the epoch for a Date Object as mentioned above. For example, the following code displays the number of shopping days left until Christmas:
This example creates a Date Object named now that contains today's date. It then creates a Date Object named nextXmas, and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and nextXmas, using getTime(), and rounding to a whole number of days. The parse() Method is useful for assigning values from date strings to existing Date Objects. For example, the following code uses parse() and setTime() to assign a date to the IPOdate object.
Dealing with dates is one of the most tedious tasks in any language. This is because many people like to represent dates and times in decidedly non-decimal systems. Months come in units of 12, hours in units of 24, and minutes and seconds in units of 60. All these variations are quite illogical from the computer's standpoint. It likes to deal with nice, round numbers, preferably powers of 2, or at least multiples of 10. The Date Object has no Properties, but many Methods. In order to use the Date Object you must first understand how to construct instances of it. There are three basic methods of creating a Date instance, as follows:
NOTE: that the month is always indexed from zero, so that November is month 10. The year can also be offset by 1900, so that you can use either of these two forms
to create a Date instance named NovDate for November 23, 1990. NOTE that for the year 2000 and beyond you must use the second form. This form may optionally take an additional three integer arguments for the time, so that 1:05 PM on November 23, 1990 is
The Date object has a large set of methods for getting and setting the components of a date. These methods are as follows:
Date Object Methods
NOTE: Date.parse() and Date.UTC() since they are static; they may not be used with Date instances. Since they return the internal representation of dates, these values are often simply passed to setTime(). |