1

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

2
  • Don't re-invent the wheel :) moment.js Commented Nov 8, 2012 at 19:36
  • If he really just needs the difference at one point building it yourself might be better than including yet another library. Plus it's nice exercise. :D Commented Nov 8, 2012 at 19:53

2 Answers 2

2

I think you are getting wrong diff value because of the millisecond part in the date delimited by .. Its not being accepted correctly by the data parser.

Try using the date and time part excluding the milliseconds as below:

 var diff = Date.parse(time2.split(".")[0]) - Date.parse( time1.split(".")[0]);

Also while you are getting wrong difference diff, your time computation is also wrong.

It should be:

       var second = Math.floor(diff /1000);
        //convert the seconds into minutes and remainder is updated seconds value
       var minute = Math.floor(second /60);
       second = second % 60;

        //convert the minutes into hours and remainder is updated minutes value
       var hour = Math.floor(minute/60);
       minute = minute %60;

       var total_time= hour+":" minute+":"+second;
Sign up to request clarification or add additional context in comments.

4 Comments

Well when I run your or mine code on jsfiddle it puts 2:35:9 out which is clearly the correct difference, including the . for the milliseconds.
thank you, your code is working also but @clentfort has a lower rating so I'll raise his rating a bit :)
@clentfort: I don't see any difference in both the answers in the way you are computing the difference. As I highlighted, its parser issue, which is not coming when you use new Date().
Thanks. :) I actually prefer this method above mine since it is not that verbose. I choose the more verbose way to show you where the problem was.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.