Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 417203 of Date.prototype.getTime()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Date/getTime
  • Revision title: Date.getTime
  • Revision id: 417203
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment JavaScript/Reference/Global_Objects/Date/getTime Web/JavaScript/Reference/Global_Objects/Date/getTime

Revision Content

Summary

Returns the numeric value corresponding to the time for the specified date according to universal time.

Method of Date
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

getTime() 

Parameters

None.

Description

The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC. You can use this method to help assign a date and time to another Date object.

This method is functionally equivalent to the valueOf method.

Examples

Using getTime for copying dates

Constructing a date object with the identical time value.

var birthday = new Date(1994, 12, 10);
var copy = new Date();
copy.setTime(birthday.getTime());

Measuring execution time

Subtracting two subsequent getTime calls on newly generated Date objects, give the time span between these two calls. This can be used to calculate the executing time of some operations.

var end, start;

start = new Date();
for (var i = 0; i < 1000; i++)
  Math.sqrt(i);
end = new Date();

console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the numeric value corresponding to the time for the specified date according to universal time.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date" title="JavaScript/Reference/Global_Objects/Date"><code>Date</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>getTime() </code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h2 id="Description" name="Description">Description</h2>
<p>The value returned by the <code>getTime</code> method is the number of milliseconds since 1 January 1970 00:00:00 UTC. You can use this method to help assign a date and time to another <code>Date</code> object.</p>
<p>This method is functionally equivalent to the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/valueOf" title="JavaScript/Reference/Global_Objects/Date/valueOf">valueOf</a> method.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Using_getTime_for_copying_dates">Using getTime for copying dates</h3>
<p>Constructing a date object with the identical time value.</p>
<pre class="brush: js">
var birthday = new Date(1994, 12, 10);
var copy = new Date();
copy.setTime(birthday.getTime());
</pre>
<h3>Measuring execution time</h3>
<p>Subtracting two subsequent getTime calls on newly generated Date objects, give the time span between these two calls. This can be used to calculate the executing time of some operations.</p>
<pre class="brush:js">
var end, start;

start = new Date();
for (var i = 0; i &lt; 1000; i++)
  Math.sqrt(i);
end = new Date();

console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/getUTCHours" title="JavaScript/Reference/Global_Objects/Date/getUTCHours">getUTCHours</a>, <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/setTime" title="JavaScript/Reference/Global_Objects/Date/setTime">setTime</a>, <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Date/valueOf" title="JavaScript/Reference/Global_Objects/Date/valueOf">valueOf</a></li>
</ul>
<!--languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Date/getTime", "zh-cn": "zh-cn/JavaScript/Reference/Global_Objects/Date/getTime" } )-->
Revert to this revision