Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Short &amp; Snazzy:</h1> <pre><code>+ new Date() </code></pre> <p>A unary operator like <code>plus</code> triggers the <code>valueOf</code> method in the <code>Date</code> object and it returns the timestamp (without any alteration).</p> <p><strong>Details:</strong></p> <p>On almost all current browsers you can use <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.4" rel="noreferrer"><code>Date.now()</code></a> to get the UTC timestamp in <strong>milliseconds</strong>; a notable exception to this is IE8 and earlier (see <a href="http://kangax.github.io/compat-table/es5/#Date.now" rel="noreferrer">compatibility table</a>).</p> <p>You can easily make a shim for this, though:</p> <pre><code>if (!Date.now) { Date.now = function() { return new Date().getTime(); } } </code></pre> <p>To get the timestamp in <strong>seconds</strong>, you can use:</p> <pre><code>Math.floor(Date.now() / 1000) </code></pre> <p>Or alternatively you could use:</p> <pre><code>Date.now() / 1000 | 0 </code></pre> <p>Which should be slightly faster, but also less readable (also <a href="https://stackoverflow.com/questions/7487977/using-bitwise-or-0-to-floor-a-number">see this answer</a>).</p> <p>I would recommend using <code>Date.now()</code> (with compatibility shim). It's slightly better because it's shorter &amp; doesn't create a new <code>Date</code> object. However, if you don't want a shim &amp; maximum compatibility, you could use the "old" method to get the timestamp in <strong>milliseconds</strong>:</p> <pre><code>new Date().getTime() </code></pre> <p>Which you can then convert to seconds like this:</p> <pre><code>Math.round(new Date().getTime()/1000) </code></pre> <p>And you can also use the <code>valueOf</code> method which we showed above:</p> <pre><code>new Date().valueOf() </code></pre> <p><strong>Timestamp in Milliseconds</strong></p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var timeStampInMs = window.performance &amp;&amp; window.performance.now &amp;&amp; window.performance.timing &amp;&amp; window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now(); console.log(timeStampInMs, Date.now());</code></pre> </div> </div> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload