我想找到两个日期之间的区别。我试过这段代码,但它给了我错误的价值。我希望获得两个日期之间的总分钟数,因此我将小时数转换为分钟数并添加到分钟数。
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 (India Standard Time)
,
且timeStart
为Mon Jan 01 2007 11:00:00 GMT+0530 (India Standard Time)
。
如果我得到1
小时差异,那么它应该是0
,而我得到的30
分钟是正确的。但是时间应该是0
。我在这里做错了吗?
答案 0 :(得分:21)
尝试此代码(使用ms作为初始单位)
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/
答案 1 :(得分:4)
试试这个:
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));
}
这是工作fiddle。
答案 2 :(得分:3)
尝试:
var diffHrs = Math.floor((hourDiff % 86400000) / 3600000);
Math.round
将0.5
小时差异调整为1
。您只想获得小时变量中的“完整”小时数,是否使用Math.floor()
答案 3 :(得分:1)
您可以尝试以下操作:-
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
答案 4 :(得分:0)
如果您确信差异将小于24小时,则以下情况有效。
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是01:26:48.677,diffHours是01
答案 5 :(得分: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;
}
答案 6 :(得分:0)
您可以通过使用以下内容获得小时分钟和倒计时之间的时差:
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;
希望这个帮助。感谢
答案 7 :(得分:0)
您可以按照以下小时和分钟格式获取时差
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