Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime" rel="noreferrer"><code>Date.prototype.getTime</code></a> method returns the number of milliseconds since the epoch (1970-01-01T00:00:00Z); it is unique to the Date type and an important method.</p> <p>The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf" rel="noreferrer"><code>Object.prototype.valueOf</code> method</a> is used to get the "primitive value" of any object. For the Date class, it is convenient to use the "time" attribute (the value returned by <code>getTime()</code>) as its primitive form since it is a common representation for dates. Moreover, it lets you use arithmetic operators on date objects so you can compare them simply by using comparison operators (<code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, etc).</p> <pre class="lang-javascript prettyprint-override"><code>var d = new Date(); d.getTime(); // =&gt; 1331759119227 d.valueOf(); // =&gt; 1331759119227 +d; // =&gt; 1331759119227 (implicitly calls "valueOf") var d2 = new Date(); (d &lt; d2); // =&gt; true (d came before d2) </code></pre> <p>Note that you could implement the "valueOf" method for your own types to do interesting things:</p> <pre class="lang-javascript prettyprint-override"><code>function Person(name, age) {this.name=name; this.age=age;} Person.prototype.valueOf = function() {return this.age; } var youngster = new Person('Jimmy', 12); var oldtimer = new Person('Hank', 73); (youngster &lt; oldtimer); // =&gt; true youngster + oldtimer; // =&gt; 85 </code></pre>
 

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