Difference between two dates in minute, hours javascript

前端 未结 8 590
野性不改
野性不改 2020-12-03 08:33

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 min

8条回答
  •  [愿得一人]
    2020-12-03 09:14

    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.

提交回复
热议问题