MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla

Revision 484381 of Date.now()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Date/now
  • Revision title: Date.now()
  • Revision id: 484381
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Date")}}

Summary

The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

Syntax

var timeInMs = Date.now();

Parameters

None.

Description

The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a {{jsxref("Global_Objects/Number", "Number")}}.

Because now is a static method of Date, you always use it as Date.now().

Polyfill

This method was standardized in ECMA-262 5th edition. Engines which have not been updated to support this method can work around the absence of this method using the following shim:

if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}

Specifications

Specification Status Comment
{{SpecName('ES5.1', '#sec-15.9.4.4', 'Date.now')}}
Implemented in JavaScript 1.5
{{Spec2('ES5.1')}} Initial definition.
{{SpecName('ES6', '#sec-date.now', 'Date.now')}} {{Spec2('ES6')}}  

Browser compatibility

Based on Kangax's compat table.

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 5 3 9 10.50 4
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

  • {{domxref("window.performance.now")}} - provides timestamps with sub-millisecond resolution for use in measuring web page performance
  • {{domxref("console.time")}} / {{domxref("console.timeEnd")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Date")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The<strong><code> Date.now()</code></strong> method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
var timeInMs = Date.now();
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>now()</code> method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a {{jsxref("Global_Objects/Number", "Number")}}.</p>
<p>Because <code>now</code> is a static method of <code>Date</code>, you always use it as <code>Date.now()</code>.</p>
<h2 id="Compatibility" name="Compatibility">Polyfill</h2>
<p>This method was standardized in ECMA-262 5<sup>th</sup> edition. Engines which have not been updated to support this method can work around the absence of this method using the following shim:</p>
<pre class="brush: js">
if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.9.4.4', 'Date.now')}}<br />
    Implemented in JavaScript 1.5</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.now', 'Date.now')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a href="http://kangax.github.com/es5-compat-table/">Kangax's compat table</a>.</p>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>5</td>
    <td>3</td>
    <td>9</td>
    <td>10.50</td>
    <td>4</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>
<h2 id="See_also">See also</h2>
<ul>
 <li>{{domxref("window.performance.now")}} - provides timestamps with sub-millisecond resolution for use in measuring web page performance</li>
 <li>{{domxref("console.time")}} / {{domxref("console.timeEnd")}}</li>
</ul>
Revert to this revision