Difference between two dates in minute, hours javascript

前端 未结 8 575
野性不改
野性不改 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:20

    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

    0 讨论(0)
  • 2020-12-03 09:29

    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

    0 讨论(0)
提交回复
热议问题