Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用简单的javascript/jquery以hh:mm:ss计算时差_Javascript_Jquery_Time - Fatal编程技术网

使用简单的javascript/jquery以hh:mm:ss计算时差

使用简单的javascript/jquery以hh:mm:ss计算时差,javascript,jquery,time,Javascript,Jquery,Time,我有两个输入字段用于时间开始和时间结束,还有一个字段用于差异,即通话持续时间。我希望字段的结果采用以下格式=00:21:03 <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_for

我有两个输入字段用于时间开始和时间结束,还有一个字段用于差异,即通话持续时间。我希望字段的结果采用以下格式=00:21:03

<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>

假设您将其作为字符串获取,您可以大致执行以下操作

var str1=“11:31:51上午”;
var str2=“上午11:52:54”;
str1=str1.split(“:”);
str2=str2.split(“:”);
if(str1[2].split(“”).pop()!=str2[2].split(“”).pop()){//一个是AM,另一个是PM
str1[0]=parseInt(str1[0])+12;
}
var finalStr=m(str1[0],str2[0])+“:”+m(str1[1],str2[1])+“:”+m(str1[2],str2[2]);
控制台日志(finalStr);
函数m(n1,n2){
返回Math.abs(parseInt(n1)-parseInt(n2));

}
您可以使用以下代码:

功能差异(a、b){
函数toTime(a){
返回日期.parse('1970-01-01'+a.substr(0,8))/1000
+(a.包括(“PM”)和(12*60*60));
}
var d=总时间(b)-总时间(a);
返回d>=0?新日期(0,0,0,0,0,d).toTimeString().substr(0,8):“”;
}
$('.qaw_form_input3')。on('input',function(){
$('#表格_qa_callduration')。val(
diff($('form#qa_startcall').val(),$('form#qa_endcall').val());
})。单击()

通话开始
通话结束
通话时长

请显示您尝试使用的HTML和代码。
新日期(new Date()-new Date()).toISOString().split(“T”)[1]。split(“.”[0]
上述“中间日期”应为新旧日期值……非常感谢您的代码。它正在工作。关于通话持续时间的输入,最初他们希望手动输入差异,但昨天他们希望自动计算,因此我可能不得不在今天将其设置为只读。:)用户及其(不断变化的)需求:我们都知道这对我们的耐心是一个多么大的挑战;-)
$("#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);
});