Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's pretty simple to convert your date string to a format that will give the expected result ('yyyy/mm/dd' or 'yyyy-mm-dd'):</p> <pre><code>new Date("21/11/1968".split('/').reverse().join('/')); </code></pre> <p>[<strong>edit</strong>] You may like this more generic method (part of the <a href="https://www.npmjs.com/package/purehelpers" rel="nofollow noreferrer">npm PureHelpers library</a>):</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>document.querySelector("#result").textContent = ` tryParseDate("2017/03/22", "ymd"); // ${tryParseDate("2017/03/22", "ymd")} tryParseDate("03/22/2017", "mdy"); // ${tryParseDate("03/22/2017", "mdy")} tryParseDate("22-03-2017", "dmy"); // ${tryParseDate("22-03-2017", "dmy")} `; function tryParseDate(dateStringCandidateValue, format = "dmy") { if (!dateStringCandidateValue) { return null; } const mapFormat = format.split("").reduce(function(a, b, i) { a[b] = i; return a; }, {}); const dateStr2Array = dateStringCandidateValue.split(/[ :\-\/]/g); const datePart = dateStr2Array.slice(0, 3); const datePartFormatted = [ +datePart[mapFormat.y], +datePart[mapFormat.m] - 1, +datePart[mapFormat.d] ]; if (dateStr2Array.length &gt; 3) { dateStr2Array.slice(3).forEach(t =&gt; datePartFormatted.push(+t)); } const dateTrial = new Date(Date.UTC.apply(null, datePartFormatted)); return dateTrial &amp;&amp; dateTrial.getFullYear() === datePartFormatted[0] &amp;&amp; dateTrial.getMonth() === datePartFormatted[1] &amp;&amp; dateTrial.getDate() === datePartFormatted[2] ? dateTrial : null; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;pre id="result"&gt;&lt;/pre&gt;</code></pre> </div> </div> </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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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