10

I want to find difference between two dates. I have tried this code but it gives me wrong values. I want get total minutes between two dates, so I am converting hours to minutes and adding to minutes.

var hourDiff = timeEnd - timeStart;
var diffHrs = Math.round((hourDiff % 86400000) / 3600000);
var diffMins = Math.round(((hourDiff % 86400000) % 3600000) / 60000);
diffMins = diffMins + (diffHrs * 60);

Here timeEnd is Mon Jan 01 2007 11:30:00 GMT+0530 (India Standard Time),

and timeStart is Mon Jan 01 2007 11:00:00 GMT+0530 (India Standard Time).

Here if hours difference I am getting 1, it should be 0 and minutes I am getting 30 that is right. But hours should be 0. Am I doing something wrong here?

bobnoble
  • 5,794
  • 3
  • 25
  • 32
sp_m
  • 2,647
  • 8
  • 38
  • 62

8 Answers8

28

Try this code (uses ms as initial units)

var timeStart = new Date("Mon Jan 01 2007 11:00:00 GMT+0530").getTime();
var timeEnd = new Date("Mon Jan 01 2007 11:30:00 GMT+0530").getTime();
var hourDiff = timeEnd - timeStart; //in ms
var secDiff = hourDiff / 1000; //in s
var minDiff = hourDiff / 60 / 1000; //in minutes
var hDiff = hourDiff / 3600 / 1000; //in hours
var humanReadable = {};
humanReadable.hours = Math.floor(hDiff);
humanReadable.minutes = minDiff - 60 * humanReadable.hours;
console.log(humanReadable); //{hours: 0, minutes: 30}

JSFiddle: http://jsfiddle.net/n2WgW/

strah
  • 6,702
  • 4
  • 33
  • 45
  • This comes down to the same suggestion as I made: "Use `Math.floor()`". Try to explain why your answer works, instead of only dumping some code in an answer. – Cerbrus Dec 30 '13 at 13:36
  • @Cerbrus yes, that's true, it's about `Math.floor`. My answer elaborates a bit the way the ms are converted into seconds, minutes, and hours. That's it. – strah Dec 30 '13 at 15:05
  • It doesn't explain why this code results in the answer the user wants. You're just using a different method to get the data. – Cerbrus Dec 30 '13 at 15:18
4

Try:

var diffHrs = Math.floor((hourDiff % 86400000) / 3600000);

Math.round rounded the 0.5 hour difference up to 1. You only want to get the "full" hours in your hours variable, do you remove all the minutes from the variable with the Math.floor()

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
4

Try this:

var startDate = new Date('Jan 01 2007 11:00:00');
var endDate = new Date('Jan 01 2007 11:30:00');
var starthour = parseInt(startDate.getHours());
var endhour = parseInt(endDate.getHours());

if(starthour>endhour){
    alert('Hours diff:' + parseInt(starthour-endhour));
}
else{
    alert('Hours diff:' + parseInt(endhour-starthour));
}

And here is the working fiddle.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
nrsharma
  • 2,532
  • 3
  • 20
  • 36
  • Use `Math.floor()` instead of `parseInt()` if you want to remove the decimal part of a number. (Which basically takes us back to my answer) [jsperf test](http://jsperf.com/parseint-vs-math-floor) – Cerbrus Dec 30 '13 at 13:15
1

If you are confident that the difference will be less that 24 hours, the following works.

var timeStart= new Date('2015-01-01 03:45:45.890');
var timeEnd = new Date('2015-01-01 05:12:34.567');
var timeDiff = new Date(timeEnd.getTime() - timeStart.getTime());
var humanTime = timeDiff.toISOString().substring(11, 23);
var diffHours = timeDiff.toISOString().substring(11, 12);

humanTime is 01:26:48.677, diffHours is 01

john nowlin
  • 135
  • 10
1

You can Try This:-

function diff_hours(dt2, dt1) 
 {

  var diff =(dt2.getTime() - dt1.getTime()) / 1000;
  diff /= (60 * 60);         // For Hours
  //diff /= (60);          For Minutes
  return Math.abs(Math.round(diff));

 }

dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,3);
console.log(diff_hours(dt1, dt2));            // you will get '24' hours


dt1 = new Date("October 13, 2014 08:11:00");
dt2 = new Date("October 13, 2014 11:13:00");
console.log(diff_hours(dt1, dt2));           // you will get '3' hours
Vishal Thakur
  • 1,564
  • 16
  • 25
0
var timeDiff = function (date1, date2) {
        var a = new Date(date1).getTime(),
            b = new Date(date2).getTime(),
            diff = {};

        diff.milliseconds = a > b ? a % b : b % a;
        diff.seconds = diff.milliseconds / 1000;
        diff.minutes = diff.seconds / 60;
        diff.hours = diff.minutes / 60;
        diff.days = diff.hours / 24;
        diff.weeks = diff.days / 7;

        return diff;
    }
Ade
  • 181
  • 1
  • 1
  • 14
0

You can get Time difference in hours minutes and secons like countdown by using following:

var diff = EndedTime - StartedTime;
var hours   = Math.floor(diff / 3.6e6);
var minutes = Math.floor((diff % 3.6e6) / 6e4);
var seconds = Math.floor((diff % 6e4) / 1000);
var duration = hours+":"+minutes+":"+seconds;

Hope This Help. Thanks

Vishal Patel
  • 1,715
  • 16
  • 26
0

You can get Time Difference in hours and minutes format like below

const startTime = new Date(event.startTime);
const endTime = new Date(event.endTime);
const diff = endTime.getTime() - startTime.getTime();
const hrDiff = diff / 3600 / 1000; // 1.555555
const totalHours = parseFloat((hrDiff).toFixed(2)); // 1.5