Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Other answers are correct, however browsers do not consistently parse date strings (see below) so you should manually parse the strings. The format in the OP is not consistent with the format in ES5 either, and will not be correctly parsed by some browsers. A simple function to convert the OP format to a date that will work in all browsers is:</p> <pre><code>function stringToDate(s) { s = s.split(/[-\/\. :]/); return new Date(s[0], --s[1], s[2], s[3], s[4], s[5], 0) } </code></pre> <p>There are some formats that are parsed by all browsers, however they are not standards compliant and if used, are dependent on all implementations continuing to support unspecified formats.</p> <h3>Edit</h3> <p>To tolerate malformed input (extra whitespace, different seperators, extra characters), <code>match</code> can be used instead of <code>split</code>:</p> <pre><code>function stringToDate(s) { s = s.match(/\d+/g); return new Date(s[0], --s[1], s[2], s[3], s[4], s[5], 0) } </code></pre> <p>However, since the values are coming from a database and a format has been specified, the date strings should fit the format. Erroneous data should not be stored in the first place.</p> <p>For completeness, here's a function to do what you want:</p> <pre><code>/* Return difference between two dates as minutes and seconds mm:ss. * Input format is yyyy-mm-dd hh:mm:ss * If strings don't converted to dates (incorrect format or invalid values), * return undefined. */ function diffInMins(s0, s1) { var diff; // Convert strings to date ojbects var d0 = stringToDate(s0); var d1 = stringToDate(s1); // Helper function to padd with leading zero function z(n) { return (n&lt;10? '0' : '') + n; } // Check conversions if (d0 &amp;&amp; d1 &amp;&amp; typeof d0 == 'object' &amp;&amp; typeof d1 == 'object') { // Return difference formatted as mm:ss if (d0 &amp;&amp; d1) { diff = d1 - d0; return z(diff/60000 | 0) + ':' + z(Math.round(diff%60000/1000)); } } } </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