Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:56:17+00:00 2026-06-12T16:56:17+00:00

I’m have a startDate and a endDate stored in a SQLite database and need

  • 0

I’m have a startDate and a endDate stored in a SQLite database and need to calculate the difference in minutes and seconds between the 2 datetimes using javascript.

For example:

startDate = 2012-10-07 11:01:13
endDate = 2012-10-07 12:42:13

I’ve had a good read though loads of similar questions on SO but could only find things relating to calculating this as part of a select.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:56:19+00:00Added an answer on June 12, 2026 at 4:56 pm

    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).

    However, please also note…

    new Date(‘2012-10-07 12:42:13’)

    … that this is not a reliable way to construct the Date from your SQLite date strings.

    When you feed a string into the constructor of a Date object, you are effectively calling Date.parse(). That behaves differently on different browsers.

    Check this out:

    > new Date('1-1-2012');
    Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
    
    > new Date('01-01-2012');
    Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
    
    > new Date('2012-1-1');
    Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
    

    Looks pretty good, right? But that’s on Chrome.

    Now check out what happens in an up-to-date version of Firefox, with the exact same calls:

    > new Date('1-1-2012');
    Date {Invalid Date}
    
    > new Date('01-01-2012');
    Date {Invalid Date}
    
    > new Date('2012-1-1');
    Date {Invalid Date}
    
    > new Date('2012-01-01');
    Date {Sat Dec 31 2011 16:00:00 GMT-0800 (PST)}
    

    Furthermore, look at this behavior, in both browsers:

    > new Date('2012-01-01');
    Sat Dec 31 2011 16:00:00 GMT-0800 (PST)
    

    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:

    > new Date('2012-01-01T00:00:00-08:00')
    Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
    

    Basically, dealing with date string parsing is a headache. You don’t want to have to digest and account for specs like this, this, and this.

    So, here’s a better alternative — 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.

    new Date(year, month, day [, hour, minute, second, millisecond])

    Here’s what that initialization could look like for your case:

    // 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);
    }
    

    Note: be careful of timezone considerations. You don’t seem to have any timezone data in that SQLite format snippet.

    Update

    @james-j clarified that he’s looking for minutes and seconds specifically.

    Here’s a snippet to extract just minutes and seconds:

    var msPerMin = 1000 * 60;
    var min = Math.floor(timeInMs / msPerMin);
    var timeInMs = timeInMs - (min * msPerMin);
    var sec = Math.floor(timeInMs / 1000 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.