以分钟,小时为单位的两个日期之间的差异javascript [英] 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);
这里 timeEnd
是 Mon Jan 01 2007 11:30:00 GMT+0530(印度标准时间)
,
和 timeStart
是 Mon Jan 01 2007 11:00:00 GMT+0530(印度标准时间)
.
如果小时差我得到 1
,它应该是 0
和分钟我得到 30
是正确的.但是小时应该是0
.我在这里做错了吗?
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?
推荐答案
尝试:
var diffHrs = Math.floor((hourDiff % 86400000) / 3600000);
Math.round
将 0.5
小时差四舍五入到 1
.您只想获得小时变量中的完整"小时数,是否使用 Math.floor()
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()
这篇关于以分钟,小时为单位的两个日期之间的差异javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!