Sets date/time of Date object.

Syntax

<oRef>.setTime(<expN>)

<oRef>

The Date object whose time you want to set.

<expN>

The number of milliseconds since January 1, 1970 00:00:00 GMT for the desired date/time.

Property of

Date

Description

While you may use standard date/time nomenclature when creating a new Date object, the setTime( ) method requires a number of milliseconds. Therefore the setTime( ) method is used primarily to copy the date/time from one Date object to another. If you tried copying dates like this:

d1 = new Date( "Aug 24 1996" )

d2 = new Date( )

d2 = d1 // Copy date

what you’re actually doing is copying an object reference for the first Date object into another variable. Both variables now point to the same object, so changing the date/time in one would appear to change the date/time in the other.

To actually copy the date/time, use the setTime( ) and getTime( ) methods:

d1 = new Date( "Aug 24 1996" )

d2 = new Date( )

d2.setTime( d1.getTime( ) ) // Copy date

If you’re copying the date/time when you’re creating the second Date object, you can use the millisecond value in the Date class constructor:

d1 = new Date( "Aug 24 1996" )

d2 = new Date( d1.getTime( ) ) // Create copy of date

You may also perform date math by adding or subtracting milliseconds from the value.