Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All the answers are generally correct with respect to converting the dates to milliseconds in epoch time, subtracting those, and converting the result back from milliseconds into your required units (although the specific implementations offered elsewhere do not currently give you exactly what you asked for -- i.e., just the number of minutes and seconds between your two datetimes).</p> <p>However, please also note...</p> <blockquote> <p>new Date('2012-10-07 12:42:13')</p> </blockquote> <p>... that this is not a reliable way to construct the Date from your SQLite date strings.</p> <p>When you feed a string into the constructor of a Date object, you are effectively calling <code>Date.parse()</code>. That behaves differently on different browsers.</p> <p>Check this out:</p> <pre><code>&gt; new Date('1-1-2012'); Sun Jan 01 2012 00:00:00 GMT-0800 (PST) &gt; new Date('01-01-2012'); Sun Jan 01 2012 00:00:00 GMT-0800 (PST) &gt; new Date('2012-1-1'); Sun Jan 01 2012 00:00:00 GMT-0800 (PST) </code></pre> <p>Looks pretty good, right? But that's on Chrome.</p> <p>Now check out what happens in an up-to-date version of Firefox, with the exact same calls:</p> <pre><code>&gt; new Date('1-1-2012'); Date {Invalid Date} &gt; new Date('01-01-2012'); Date {Invalid Date} &gt; new Date('2012-1-1'); Date {Invalid Date} &gt; new Date('2012-01-01'); Date {Sat Dec 31 2011 16:00:00 GMT-0800 (PST)} </code></pre> <p>Furthermore, look at this behavior, in both browsers:</p> <pre><code>&gt; new Date('2012-01-01'); Sat Dec 31 2011 16:00:00 GMT-0800 (PST) </code></pre> <p>Simply prepending zeroes to the month and date digits causes a time warp! You have to set the time and a timezone (for me, PST) to make that go away:</p> <pre><code>&gt; new Date('2012-01-01T00:00:00-08:00') Sun Jan 01 2012 00:00:00 GMT-0800 (PST) </code></pre> <p>Basically, dealing with date string parsing is a headache. You don't want to have to digest and account for specs like <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse" rel="nofollow">this</a>, <a href="http://tools.ietf.org/html/rfc2822#page-14" rel="nofollow">this</a>, and <a href="http://www.w3.org/TR/NOTE-datetime" rel="nofollow">this</a>.</p> <p>So, <strong>here's a better alternative</strong> -- pass the parts of your datetime as separate args to the constructor of the Date object. That will reliably create the date for you, so your subsequent comparisons are valid.</p> <blockquote> <p>new Date(year, month, day [, hour, minute, second, millisecond])</p> </blockquote> <p>Here's what that initialization could look like for your case:</p> <pre><code>// Extract month, day, year from SQLite format, 'trimming' whitespace. var sqlLiteSampleStr = "2012-10-07 11:01:13"; var re = /^\s*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\s*$/; var match = re.exec(sqlLiteSampleStr); if (match) { var year = parseInt(match[1]); var month = parseInt(match[2]) - 1; // Zero-indexed months. var date = parseInt(match[3]); var hour = parseInt(match[4]); var minute = parseInt(match[5]); var second = parseInt(match[6]); var date = new Date(year, month, date, hour, minute, second); } </code></pre> <p>Note: be careful of timezone considerations. You don't seem to have any timezone data in that SQLite format snippet.</p> <p><strong>Update</strong></p> <p>@james-j clarified that he's looking for minutes and seconds specifically. </p> <p>Here's a snippet to extract just minutes and seconds:</p> <pre><code>var msPerMin = 1000 * 60; var min = Math.floor(timeInMs / msPerMin); var timeInMs = timeInMs - (min * msPerMin); var sec = Math.floor(timeInMs / 1000 ); </code></pre>
    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.
 

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