Date.now()

  • Revision slug: JavaScript/Reference/Global_Objects/Date/now
  • Revision title: Date.now
  • Revision id: 376311
  • Created:
  • Creator: MattBrubeck
  • Is current revision? No
  • Comment add link to window.performance.now

Revision Content

Introduction

Returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

Method of Date
Implemented in JavaScript 1.5
ECMAScript Edition ECMAScript 5th Edition

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 number.

When using now() to create timestamps or unique IDs, keep in mind that the resolution may be 15 milliseconds on Windows (see {{ Bug(363258) }}), so you could end up with several equal values if now is called multiple times within a short time span.

Compatibility

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();
  };
}

See also

window.performance.now
provides timestamps with sub-millisecond resolution for use in measuring web page performance

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 Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

 

Revision Source

<h2 id="Introduction">Introduction</h2>
<p>Returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.</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.5</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 5th Edition</td>
    </tr>
  </tbody>
</table>
<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 <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number" title="JavaScript/Reference/Global_Objects/Number">number</a>.</p>
<p>When using <code>now()</code> to create timestamps or unique IDs, keep in mind that the resolution may be 15 milliseconds on Windows (see {{ Bug(363258) }}), so you could end up with several equal values if <code>now</code> is called multiple times within a short time span.</p>
<h2 id="Compatibility" name="Compatibility">Compatibility</h2>
<p>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:</p>
<pre class="brush: js">
if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}
</pre>
<h2>See also</h2>
<dl>
  <dt>
    <a href="/en-US/docs/DOM/window.performance.now" title="/en-US/docs/DOM/window.performance.now">window.performance.now</a></dt>
  <dd>
    provides timestamps with sub-millisecond resolution for use in measuring web page performance</dd>
</dl>
<ul>
</ul>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a class="external" 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>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>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision