Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Custom formatting function:</h2> <p>For fixed formats, a simple function make the job. Following example generate the international format YYYY-MM-DD:</p> <pre><code>function dateToYMD(date) { var d = date.getDate(); var m = date.getMonth() + 1; var y = date.getFullYear(); return '' + y + '-' + (m&lt;=9 ? '0' + m : m) + '-' + (d &lt;= 9 ? '0' + d : d); } </code></pre> <p>Note: It is, however, usually not a good idea to extend the Javascript standard libraries (e.g. by adding this function to the prototype of Date). </p> <p>A more advanced function could generate configurable output based on a format parameter. There are a couple of good examples in this same page.</p> <p>If to write a formatting function is too long, there are plenty of libraries around which does it. Some other answers already enumerate them. But increasing dependencies also has it counter-part.</p> <h2>Standard ECMAScript formatting functions:</h2> <p>Since more recent versions of ECMAscript, the <code>Date</code> class has some specific formatting functions:</p> <blockquote> <p><strong>toDateString</strong>: Implementation dependent, show only the date.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.todatestring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.todatestring</a></p> <pre><code>new Date().toDateString(); // e.g. "Fri Nov 11 2016" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toISOString</strong>: Show ISO 8601 date and time.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.toisostring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.toisostring</a></p> <pre><code>new Date().toISOString(); // e.g. "2016-11-21T08:00:00.000Z" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toJSON</strong>: Stringifier for JSON. </p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tojson" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tojson</a></p> <pre><code>new Date().toJSON(); // e.g. "2016-11-21T08:00:00.000Z" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toLocaleDateString</strong>: Implementation dependent, a date in locale format.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaledatestring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaledatestring</a></p> <pre><code>new Date().toLocaleDateString(); // e.g. "21/11/2016" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toLocaleString</strong>: Implementation dependent, a date&amp;time in locale format.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocalestring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocalestring</a></p> <pre><code>new Date().toLocaleString(); // e.g. "21/11/2016, 08:00:00 AM" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toLocaleTimeString</strong>: Implementation dependent, a time in locale format.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaletimestring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaletimestring</a></p> <pre><code>new Date().toLocaleTimeString(); // e.g. "08:00:00 AM" </code></pre> </blockquote> <hr> <blockquote> <p><strong>toString</strong>: Generic toString for Date.</p> <p><a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tostring" rel="noreferrer">http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tostring</a></p> <pre><code>new Date().toString(); // e.g. "Fri Nov 11 2016 08:00:00 GMT+0100 (W. Europe Standard Time)" </code></pre> </blockquote> <p>Note: it is possible to generate custom output out of those formatting functions:</p> <pre><code>new Date().toISOString().slice(0,10); // By @Image72, return YYYY-MM-DD </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