JavaScript tutorial:
setTime method

 

Applies to: Date Object

Sets the date and time value in the Date object. It returns in milliseconds. To convert the millisecond to string use obj.toLocaleString( ).

Syntax

objDate.setTime(milliseconds)

The milliseconds argument is an integer value representing the number of elapsed seconds since midnight, January 1, 1970 GMT.

Return millisecond

Returns set time in milliseconds.

Example

If milliseconds is negative, it indicates a date before 1970. The range of available dates is approximately 285,616 years from either side of 1970.

Setting the date and time with the setTime method is independent of the time zone.

The following example illustrates the use of the setTime method:

function SetTime(newtime)
{
    var d, s;
    d = new Date();
    d.setTime(newtime);
    s = "Current setting is ";
    s += d.toUTCString();
    return(s);
}

document.write(SetTime(1114615678910));

To run the code above, paste it into JavaScript Editor, and click the Execute button

See also: Date Object Methods, getTime Method