我正在计算两个日期之间的时间以及到达旅行时间的时间 天和小时(四舍五入)。
我使用bootstrap(包括datepicker)的4个输入格式dd:mm:yy
和hh:mm
标记:
<input type="date" name="travel_begin_date" id="travel_begin_date" placeholder="Text input" class="span12">
<input type="time" name="travel_begin_time" id="travel_begin_time" placeholder="Text input" class="span12">
<input type="date" id="travel_end_date" name="travel_end_date" placeholder="Text input" class="span12">
<input type="time" for="travel_end_time" name="travel_end_time" placeholder="Text input" class="span12">
<div id="days_difference"></div>
<div id="hours_difference"></div>
功能
function calculateDifference() {
console.log('calculate difference start...');
var startDate = $("#travel_begin_date").val();
var startTime = $("#travel_begin_time").val();
var endDate = $("#travel_end_date").val();
var endTime = $("#travel_end_time").val();
var stDate = new Date(startDate);
var enDate = new Date(endDate);
var diff=enDate.getTime() - stDate.getTime();
var differenceDays = (diff) / (1000 * 60 * 60 * 24);
var differenceHours= ((diff) % (1000 * 60 * 60 * 24))/ (1000 * 60 * 60);
document.getElementById('days_difference').innerHTML += differenceDays + " days and";
document.getElementById('hours_difference').innerHTML += differenceHours + " hours";
}
现在的问题是:我如何计算hours_difference。
例如:
差异日期:2013年3月20日 时间:2013年3月22日10:00 时间:11:45 =结果2天2小时(四舍五入)?
是否有简单的代码或插件?
答案 0 :(得分:1)
根据评论更新了答案,并添加了 DEMO
<input type="time" id="travel_end_time" name="travel_end_time" placeholder="Text input" class="span12">
只有travel_end_time
更改
function calculateDifference() {
console.log('calculate difference start...');
var startDate = $("#travel_begin_date").val();
var startTime =(''+ $("#travel_begin_time").val()).split(":"); // Used split
var endDate = $("#travel_end_date").val();
var endTime = (''+ $("#travel_end_time").val()).split(":");// Used split
var stDate = new Date(startDate);
stDate.setHours(parseInt(startTime[0]));// Set Hours
stDate.setMinutes(parseInt(startTime[1]));// Set Minutes
var enDate = new Date(endDate);
enDate.setHours(parseInt(endTime[0]));// Set Hours
enDate.setMinutes(parseInt(endTime[1]));// Set Minutes
var diff=enDate.getTime() - stDate.getTime();
var differenceDays = (diff) / (1000 * 60 * 60 * 24);
var differenceHours= ((diff) % (1000 * 60 * 60 * 24))/ (1000 * 60 * 60);
document.getElementById('days_difference').innerHTML += Math.round(differenceDays) + " days and";
document.getElementById('hours_difference').innerHTML += Math.round(differenceHours ) + " hours";
}
答案 1 :(得分:0)
你可能需要这样的东西吗?
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M+12*d2Y)-(d1M+12*d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear()-d1.getFullYear();
}
}
答案 2 :(得分:0)
//
// Any source code blocks look like this
//
t1="10/10/2006" ;
t2="15/10/2006";
//Total time for one day
var one_day=1000*60*60*24;
//Here we need to split the inputed dates to convert them into standard format
for furter execution
var x=t1.split("/");
var y=t2.split("/");
//date format(Fullyear,month,date)
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;
//Calculate difference between the two dates, and convert to days
_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
//_Diff gives the diffrence between the two dates.