Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere can I find documentation on formatting a date in JavaScript?
    primarykey
    data
    text
    <p>I noticed that JavaScript's <code>new Date()</code> function is very smart in accepting dates in several formats.</p> <pre><code>Xmas95 = new Date("25 Dec, 1995 23:15:00") Xmas95 = new Date("2009 06 12,12:52:39") Xmas95 = new Date("20 09 2006,12:52:39") </code></pre> <p>I could not find documentation anywhere showing all the valid string formats while calling <code>new Date()</code> function.</p> <p>This is for converting a string to a date. If we look at the opposite side, that is, converting a date object to a string, until now I was under the impression that JavaScript doesn't have a built-in API to format a date object into a string.</p> <blockquote> <p><strong>Editor's note:</strong> The following approach is the asker's attempt that worked on a particular browser but does <em>not</em> work in general; <strong>see the answers on this page</strong> to see some actual solutions.</p> </blockquote> <p>Today, I played with the <code>toString()</code> method on the date object and surprisingly it serves the purpose of formatting date to strings.</p> <pre><code>var d1 = new Date(); d1.toString('yyyy-MM-dd'); //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome d1.toString('dddd, MMMM ,yyyy') //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome </code></pre> <p>Also here I couldn't find any documentation on all the ways we can format the date object into a string.</p> <p>Where is the documentation which lists the format specifiers supported by the <code>Date()</code> object?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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