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 8818081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:00:43+00:00 2026-06-14T05:00:43+00:00

I need to get time difference in this format: HH:MM:SS using a Javascript. I

  • 0

I need to get time difference in this format: “HH:MM:SS” using a Javascript.

I have tried this:

var diff = Date.parse( time2) - Date.parse( time1 );
var total_time = (diff / 1000 / 60 / 60) + ":" + (diff / 1000 / 60) + ":" + (diff / 1000);

and this:

var diff = new Date( time2) - new Date( time1 );
var total_time = (diff / 1000 / 60 / 60) + ":" + (diff / 1000 / 60) + ":" + (diff / 1000);

These are the values of time2 and time1:

time1: "2012-11-07 15:20:32.161"
time2: "2012-11-07 17:55:41.451"

And result I am getting in both cases is:

total_time= 0.5250819444444444:31.504916666666666:1890.295

Which you can see is not correct

  • 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-14T05:00:44+00:00Added an answer on June 14, 2026 at 5:00 am

    You forgot to remove the number of milliseconds you already calculated from diff. Here is a very verbose example on how you do it in a propper way.

    var time1 = "2012-11-07 15:20:32.161",
        time2 = "2012-11-07 17:55:41.451",
    
        SECOND = 1000,
        MINUTE = SECOND* 60,
        HOUR = MINUTE* 60;
    
    var diff = new Date(time2) - new Date(time1);
    
    var hours = Math.floor(diff / HOUR); // Calculate how many times a full hour fits into diff
    diff = diff - (hours * HOUR); // Remove hours from difference, we already caluclated those
    var minutes = Math.floor(diff / MINUTE); // Calculate how many times a full minute fits into diff
    diff = diff - (minutes * MINUTE); // Remove minutes from difference
    var seconds = Math.floor(diff / SECOND); // As before
    diff = diff - (seconds * SECOND);
    var rest = diff;
    
    var total_time = hours + ":" + minutes + ":" + seconds + " " + rest ;
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby application where i need to get date-time difference in Days-Hours-Minutes
I have linq request. I need get item.Title in select. how do this? var
I need to get real world date and time in Java. I use: Date
Question: I need to get the DATE ONLY (= date WITHOUT time) with an
how can I get the current time in hh:mm format? I need to be
I need get all items these have no categories int? categoryId = null; var
my application, written in Java, need to get notified each time internet connection is
How can I get current time (I need hours, minutes, seconds) crossplatform in c++?
I need to update time info in a DateTime. I get a string in
I have two times, a start and a stop time, in the format of

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.