0

Does anyone how can I do a time difference count in Javascript?

Example:

If I have the following time (24H format)

startTime = 0615
endTime = 1530
breakTime = 0030

how can I get the following output?

diffB4Break = 7.15 hours
afterBreak = 6.45 hours
Jin Yong
  • 42,698
  • 72
  • 141
  • 187
  • `0030` - is that a string or something? If not, it would get interpreted as an octal number literal and equal 24_10 – Bergi Feb 04 '13 at 04:19

2 Answers2

1

There are complications here, for example you would need to know what the maximum shift was, and whether it is possible for a worker to be scheduled to work over the threshold of a day, i.e. start work at 2300 and finish at 0900.

It very quickly becomes clear why it's better to use a Date/Time object to handle dates and time-deltas.

In Javascript have a look at Date

jsj
  • 9,019
  • 17
  • 58
  • 103
0

Based on @Guffa's answer here:

function parseTime(s) {
   var c = s.split(':');
   return parseInt(c[0]) * 60 + parseInt(c[1]);
}
var startTime = '6:45';
var endTime = '15:30';
var diff = parseTime(endTime) - parseTime(startTime);           
var min = diff % 60;
var hrs = parseInt(diff / 60);
alert(hrs + ":" + min);
Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129