我有两个输入字段,用于时间开始和时间结束,还有另一个字段表示呼叫持续时间的差异。我想在球场上的结果是这种格式= 0时21分03秒使用简单的javascript/jquery计算hh:mm:ss的时间差异
<div class="inline_divider">
<label for="form_qa_startcall" class="qaw_form_label3">Start of Call</label>
<input type="text" id="form_qa_startcall" class="qaw_form_input3" placeholder="eg. 11:36:47 PM" maxlength="11">
</div>
<div class="inline_divider">
<label for="form_qa_endcall" class="qaw_form_label3">End of Call</label>
<input type="text" id="form_qa_endcall" class="qaw_form_input3" placeholder="eg. 11:46:47 PM" maxlength="11">
</div>
<div class="inline_divider">
<label for="form_qa_callduration" class="qaw_form_label3">Call Duration</label>
<input type="text" id="form_qa_callduration" class="qaw_form_input3" placeholder="eg. 00:01:10" maxlength="8">
</div>
我有一个hardtime得到的结果为“AM/PM”格式所致。 我发现了类似的问题,但他正在使用“momentjs”。尽可能多的,我不想使用任何插件,只是一个普通的JavaScript或从jQuery会做。这是我工作的代码,我得到了结果(0:21:3),但我需要两位数的小时,分钟和秒(00:21:03)。
$("#form_qa_endcall").on('keyup',function(){
var callStart = $('#form_qa_startcall').val();
var callEnd = $('#form_qa_endcall').val();
var timeStart = new Date("01/01/2007 " + callStart);
var timeEnd = new Date("01/01/2007 " + callEnd);
function datediff(fromDate,toDate,interval) {
var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
fromDate = new Date(fromDate);
toDate = new Date(toDate);
var timediff = toDate - fromDate;
if (isNaN(timediff)) return NaN;
switch (interval) {
case "years": return toDate.getFullYear() - fromDate.getFullYear();
case "months": return (
(toDate.getFullYear() * 12 + toDate.getMonth())
-
(fromDate.getFullYear() * 12 + fromDate.getMonth())
);
case "weeks" : return Math.floor(timediff/week);
case "days" : return Math.floor(timediff/day);
case "hours" : return Math.floor(timediff/hour);
case "minutes": return Math.floor(timediff/minute);
case "seconds": return Math.floor(timediff/second);
default: return undefined;
}
}
var seco = datediff(timeStart, timeEnd, 'seconds') % 60;
var minu = datediff(timeStart, timeEnd, 'minutes') % 60;
var hour = datediff(timeStart, timeEnd, 'hours');
$('#form_qa_callduration').val(hour + ":" + minu + ":" + seco);
});
请显示您尝试过的HTML和代码。 – trincot
'new Date(new Date() - new Date()).toISOString()。split(“T”)[1] .split(“。”)[0]' – dandavis
上面的“中间日期”应该是旧的和新的日期值... – dandavis