-4

How do I get the time difference of two values of 24hr format?

For example

var time1 = 22:30:00,
    time2 = 06:30:00;

Difference should come as 08:00:00

Albzi
  • 15,431
  • 6
  • 46
  • 63

3 Answers3

1

You are much better off to do this type of maths with full date objects, otherwise you have to make guesses about the time values such as if the finish is less that the start, it must be on the next day.

The following includes a couple of helper functions and a main function to get the difference.

// Convert h:m:s to seconds
function hmsToSecs(s) {
  var b = s.split(':');
  return b[0]*3.6e3 + b[1]*60 + +b[2];
}

// Convert seconds to hh:mm:ss
function secsToHMS(n) {
  function z(n){return (n<10? '0':'') + n;}
  var sign = n < 0? '-' : '';
  n = Math.abs(n);
  return sign + z(n/3.6e3|0) + ':' + z(n%3.6e3/60|0) + ':' + z(n%60);
}

// Calculate time difference between two times
// start and finish in hh:mm:ss
// If finish is less than start, assume it's the following day
function timeDiff(start, finish) {
  var s = hmsToSecs(start);
  var f = hmsToSecs(finish);

  // If finish is less than start, assume is next day
  // so add 24hr worth of seconds
  if (f < s) f += 8.64e4;

  return secsToHMS(f - s);
}

console.log(timeDiff('22:30:00','06:30:00')); //  08:00:00
console.log(timeDiff('06:30:00','22:30:00')); //  16:00:00

Using full date objects, you can do:

var start = new Date(2014,5,5,22,30);  // 22:30:00 on 5 June 2014
var finish = new Date(2014,5,6,6,30);  // 06:30:00 on 6 June 2014

// Subtract dates to get difference in ms, convert to seconds and format
console.log(secsToHMS((finish - start)/1000)); //  08:00:00
console.log(secsToHMS((start - finish)/1000)); // -08:00:00
RobG
  • 142,382
  • 31
  • 172
  • 209
0

I Suggest that you should use jquery date.js library and then you can use its Timespan class like below:

var future = Date.parseExact("22:30:00", "hh:mm:ss"); 

var past = Date.parseExact("06:30:00", "hh:mm:ss"); 

var span = new TimeSpan(future - now); 

and your difference in hours is as below:

span.getHours() + ":" span.getMinutes() + ":" span.getSeconds()
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
-2

If you want Diff function in C#;

DateTime oldDate= "06/01/2014 12:00:00 AM";
TimeSpan timeDiff = DateTime.Now - oldDate;

int diff =Convert.ToInt32(timeDiff.TotalHours);

if you want it in JavaScript thats a script block should help you;

  function diffDateTime(startDT, endDT) {

        if (typeof startDT == 'string' && startDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[amp ]{0,3}$/i)) {
            startDT = startDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/);
            startDT = startDT.toString().split(':');
            var obstartDT = new Date();
            obstartDT.setHours(startDT[0]);
            obstartDT.setMinutes(startDT[1]);
            obstartDT.setSeconds(startDT[2]);
        }
        else if (typeof startDT == 'string' && startDT.match(/^now$/i)) var obstartDT = new Date();
        else if (typeof startDT == 'string' && startDT.match(/^tomorrow$/i)) {
            var obstartDT = new Date();
            obstartDT.setHours(24);
            obstartDT.setMinutes(0);
            obstartDT.setSeconds(1);
        }
        else var obstartDT = new Date(startDT);

        if (typeof endDT == 'string' && endDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[amp ]{0,3}$/i)) {
            endDT = endDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/);
            endDT = endDT.toString().split(':');
            var obendDT = new Date();
            obendDT.setHours(endDT[0]);
            obendDT.setMinutes(endDT[1]);
            obendDT.setSeconds(endDT[2]);
        }
        else if (typeof endDT == 'string' && endDT.match(/^now$/i)) var obendDT = new Date();
        else if (typeof endDT == 'string' && endDT.match(/^tomorrow$/i)) {
            var obendDT = new Date();
            obendDT.setHours(24);
            obendDT.setMinutes(0);
            obendDT.setSeconds(1);
        }
        else var obendDT = new Date(endDT);
        var secondsDiff = (obendDT.getTime() - obstartDT.getTime()) > 0 ? (obendDT.getTime() - obstartDT.getTime()) / 1000 : (86400000 + obendDT.getTime() - obstartDT.getTime()) / 1000;
        secondsDiff = Math.abs(Math.floor(secondsDiff));

        var oDiff = {};     // object that will store data returned by this function

        oDiff.days = Math.floor(secondsDiff / 86400);
        oDiff.totalhours = Math.floor(secondsDiff / 3600);      // total number of hours in difference
        oDiff.totalmin = Math.floor(secondsDiff / 60);      // total number of minutes in difference
        oDiff.totalsec = secondsDiff;      // total number of seconds in difference

        secondsDiff -= oDiff.days * 86400;
        oDiff.hours = Math.floor(secondsDiff / 3600);     // number of hours after days

        secondsDiff -= oDiff.hours * 3600;
        oDiff.minutes = Math.floor(secondsDiff / 60);     // number of minutes after hours

        secondsDiff -= oDiff.minutes * 60;
        oDiff.seconds = Math.floor(secondsDiff);     // number of seconds after minutes

        return oDiff;
    }

usage;

 var objDiff = diffDateTime('06/01/2014 12:00:00 AM', 'now');
 var dtdiff = objDiff.days + ' days, ' + objDiff.hours + ' hours, ' + objDiff.minutes + ' minutes, ' + objDiff.seconds + ' seconds';

Important: You have to remember that DateTime format must be in en-US format dd/MM/yyyy hh:mm:ss.

BlueNight
  • 7
  • 1
  • 3