0

I have a jQuery clock that gives the time from 00:00 to 23:00.

Now I want to calculate the time difference between two times. Suppose the "in" time is 11:00, and the "out" time is 16:00.

How do I calculate the time difference in javascript?

user3723637
  • 130
  • 8
manishjangir
  • 1,605
  • 6
  • 24
  • 27
  • 1
    **What have you tried?** What format is the times in? (String, Date object?) How do we retrieve the time? What format do you want the date difference in? – Matt Mar 22 '12 at 10:11
  • i have simple two text boxes which gives time in string format – manishjangir Mar 22 '12 at 10:12

4 Answers4

1

You can use Date and setHours like that:

var first = new Date();
first.setHours(0, 11, 0, 0);

var second = new Date();
second.setHours(0, 16, 0, 0);

alert("Diff in seconds: " + (second - first));
Vodun
  • 1,377
  • 1
  • 10
  • 12
0

you should try momentjs, i think its much better than datejs.

According to momentjs example:

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1 
Draco
  • 262
  • 5
  • 12
0

You may prefer to use a third party JS date library such as Datejs.

Barry Kaye
  • 7,682
  • 6
  • 42
  • 64
0

As suggested from Barry, I would take a look at Datejs.

In the documentation page if you find the word "difference" you could find a guy asking for the same problem. As suggested in the comment reply you should use the TimeSpan class.

Hope it helps!

Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22