JavaScript tutorial:
getTime method

 

Applies to: Date Object

The getTime method is use to get the time value in a Date object.

Syntax

objDate.getTime()

Return

Returns the time value in a Date object.

Example

The getTime method returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object. The range of dates is approximately 285,616 years from either side of midnight, January 1, 1970. Negative numbers indicate dates prior to 1970.

When doing multiple date and time calculations, it is frequently useful to define variables equal to the number of milliseconds in a day, hour, or minute. For example:

var MinMilli = 1000 * 60
var HrMilli = MinMilli * 60
var DyMilli = HrMilli * 24

The following example illustrates the use of the getTime method:

function GetTime()
{
    var d, s, t;
    var MinMilli = 1000 * 60;
    var HrMilli = MinMilli * 60;
    var DyMilli = HrMilli * 24;
    d = new Date();
    t = d.getTime();
    s = "It's been "
    s += Math.round(t / DyMilli) + " days since 1/1/70";
    return(s);
}

document.write(GetTime());

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

See also: Date Object Methods, setTime Method