0

I have had a good search for what I'm looking for but can't seem to find exactly what I need. I'm new to jQuery and JavaScript.

All I want to do is subtract one 24 hour clock time from another to find how many hours have been allocated using JQuery or JavaScript i.e. start time: 12:00 end time: 16:00 would be 4 hours.

How would I go about doing this without having issues when going from say 12:00 till 12:00 the following day?

I am not dealing with with dates, just time.

Currently the times are stored like this as part of an object with start_time end_time:

var shift = {'location':$('#shift_location').val(),
             'shift_date': $('#shift_date').val(),
             'start_time':$('#shift_start_time').val(),
             'end_time':$('#shift_end_time').val()
            };
var shift_list = JSON.parse(localStorage.shift);
shift_list.push(shift);
localStorage.shift = JSON.stringify(shift_list);  
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
JamesF
  • 67
  • 1
  • 1
  • 9

2 Answers2

1

You can use momentjs to do things with dates in javascript.

Example (moment doc, fiddle):

var start_time = "12:00";
var end_time = "16:00";

var start = moment.duration(start_time, 'h');
var end = moment.duration(end_time, 'h');
alert(end.subtract(start).hours()); // 4

Of course, because of the simplicity of the task you could always use plain javascript:

var start_time = "12:00";
var end_time = "16:00";
alert(parseInt(end_time, 10) - parseInt(start_time, 10)); // 4
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
  • If you're new to JavaScript you should use a good library like momentjs for date time calculations and date parsing. – skmasq Jan 17 '14 at 20:16
  • thanks this is almost perfect, however if I use a time with minutes it doesn't add them up, just the hours. I imagine this is to do with the .hours so I should be able to figure it out. This answer has put me on the right track, cheers – JamesF Jan 17 '14 at 20:34
1

With the given information i.e start_time and end_time there is no way you can cover multiple days. They just oscillate between 0 to 23 hours. There is no counter involved to calculate multiple days. if you need that you need two more states which will store start_date and end_date which will act as counter as pointed by @John Boker. But if you are sure that the difference never goes beyond 24 hours then we can use the parseTime from JAVASCRIPT: subtracting Time and getting its number of minutes function with our own little modifications.

function parseTime(s) {
   var c = s.split(':');
   return parseInt(c[0]) * 60 + parseInt(c[1]);
}

var limit = parseTime("23:59");

function getDiff(start_time, end_time){
   var a = parseTime(start_time), b = parseTime(end_time);
   if(b < a) // means its the next day.
      return Math.round((limit - a + b)/60);
   else if(b > a)
      return Math.round((b - a)/60);
   else if(b - a == 0)
      return 24.0;
   else
      alert("Invalid data");
 }

 alert(getDiff("12:00", "11:00"));
Community
  • 1
  • 1
prototype
  • 30
  • 7