home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Previous Section Next Section

2.10 Calculating a Previous or Future Date

NN 2, IE 3

2.10.1 Problem

You want to obtain a date based on a specific number of days before or after a known date.

2.10.2 Solution

The basic technique is to create a date object with a known date, and then add or subtract any number of units from that known date. After that, you can read the components of the modified date object to obtain the string or numerical representation of the date.

For example, we'll calculate the date that is 10 days from the current date. After creating a date object for now, a statement reads the date component (a calendar date within the month) and then sets the date value ahead by 10 days:

var myDate = new Date( );
myDate.setDate(myDate.getDate( ) + 10);

At this point, the myDate object contains the future date in milliseconds, irrespective of months, dates, and years. But if you then read myDate's string version (or locale string in recent browsers), you see the future date correctly calculated:

document.myForm.deadline.value = myDate.toLocaleDateString( );

2.10.3 Discussion

You can move the date forward or back by any increment you like, even when it doesn't seem logical. For example, if a date object is currently pointing to the 25th of a month, you can get the date 10 days in the future by adding 10 to the date:

myDate.setDate(myDate.getDate( ) + 10);

Even though 25 plus 10 is 35, the date object corrects for the number of days in the object's month, and calculates the correct date in the following month 10 days after the 25th.

By keeping its internal workings strictly at the millisecond level, a date object can easily adapt itself to month and year boundaries. Details about the month, date, and year are calculated internally and returned only upon request. For example, you add 10 days to the 25th of June (which has 30 days), you arrive at the 5th of July; but add 10 days to the 25th of July (which has 31 days), and you reach the 4th of August. The JavaScript interpreter takes care of all such irregularities for you.

A date object has numerous functions for getting and setting components of the date, ranging from the millisecond to the year. Table 2-3 shows the most common methods and their value ranges.

Table 2-3. Date methods

Read

Write

Values

Description

getTime( )

setTime(val)

0-...

Number of milliseconds since 1Jan1970 at 00:00:00 UTC

getSeconds( )

setSeconds(val)

0-59

Number of seconds after the minute stored in the object

getMinutes( )

setMinutes(val)

0-59

Number of minutes after the hour stored in the object

getHours( )

setHours(val)

0-23

Number of hours in the date stored in the object

getDay( )

setDay(val)

0-6

Day of the week (Sunday = 0, Monday = 1, etc.)

getDate( )

setDate(val)

1-31

Date number

getMonth( )

setMonth(val)

0-11

Month in the object's year (January = 0)

getFullYear( )

setFullYear(val)

1970-...

Four-digit year

All of these methods deal with time in the client computer's local time zone. If you need to work on a more global scale, see Recipe 15.8.

2.10.4 See Also

Recipe 2.9 for creating a date object; Recipe 2.11 for calculating the number of days between two dates; Recipe 15.6, Recipe 15.7, and Recipe 15.8 for more date applications.

    Previous Section Next Section