简体   繁体   中英

Difference between two dates in minute, hours javascript

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?

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/

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()

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 .

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

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
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;
    }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM