Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a <strong>quick and dirty</strong> implementation of <code>datediff</code>, as a proof of concept to solve the problem as presented in the question. It relies on the fact that you can get the elapsed milliseconds between two dates by subtracting them, which coerces them into their primitive number value (milliseconds since the start of 1970). </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>// new Date("dateString") is browser-dependent and discouraged, so we'll write // a simple parse function for U.S. date format (which does no error checking) function parseDate(str) { var mdy = str.split('/'); return new Date(mdy[2], mdy[0]-1, mdy[1]); } function datediff(first, second) { // Take the difference between the dates and divide by milliseconds per day. // Round to nearest whole number to deal with DST. return Math.round((second-first)/(1000*60*60*24)); } alert(datediff(parseDate(first.value), parseDate(second.value)));</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;input id="first" value="1/1/2000"/&gt; &lt;input id="second" value="1/1/2001"/&gt;</code></pre> </div> </div> </p> <p>You should be aware that the "normal" Date APIs (without "UTC" in the name) operate in the local timezone of the user's browser, so in general you could run into issues if your user is in a timezone that you don't expect, and your code will have to deal with Daylight Saving Time transitions. You should carefully read the documentation for the Date object and its methods, and for anything more complicated, strongly consider using a library that offers more safe and powerful APIs for date manipulation.</p> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates" rel="noreferrer">Numbers and Dates -- MDN JavaScript Guide</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" rel="noreferrer"><code>Date</code> -- MDN JavaScript reference</a></li> </ul> <p><sub>Also, for illustration purposes, the snippet uses <a href="https://www.w3.org/TR/html5/browsers.html#named-access-on-the-window-object" rel="noreferrer">named access on the <code>window</code> object</a> for brevity, but in production you should use standardized APIs like <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById" rel="noreferrer">getElementById</a>, or more likely, some UI framework.</sub></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. 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.
    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