如何使用jQuery计算时差

时间:2017-07-12 09:47:20

标签: javascript jquery

我有计算时差的代码,效果很好。我需要更改方法(实际上我在某些条件下添加了一个方法)。我在文本框具有值Istirahat的情况下添加的方法,然后我需要将方法更改为我减去一小时的时差。

我认为在没有代码的情况下看到我的解释会很困惑。

以下是代码:



$(document).ready(function() {
    var $time1 = $("#start");
    var $time2 = $("#end");
    var $diff = $("#jam_total");

    function updateHours() {   
        var dtStart = new Date("7/20/2015 " + $time1.val());
        var dtEnd = new Date("7/20/2015 " + $time2.val());
        var stats1 = $("#status_check").val();

        if(stats1!='ISTIRAHAT') {
            var diff = ((dtEnd - dtStart)) / 1000;
        } else if(stats1!='TANPA ISTIRAHAT') {
            var diff = ((((dtEnd - dtStart)) / 1000) - 1);
        }
        var totalTime = 0;

        if (diff > 60*60*12) {
            totalTime = formatDate(60*60*12);
        } else {
            totalTime = formatDate(diff);
        }

        $diff.val(totalTime);
    }

    function formatDate(diff) {
        var hours = parseInt(diff / 3600) % 24;
        var minutes = parseInt(diff / 60) % 60;
        var seconds = diff % 60;

        return (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes);
    }

    $("#option2").on("change", function() {
        if($time1.val() && $time2.val()) {
            updateHours();
        }
    });
});
&#13;
<input type="time" id="start" name="logintime"/>
<input type="time" id="end"name="logouttime" />
<br /><br />
<select name="option2" id="option2" onchange="Choose1(this)" style="float:left">
    <option value="-">-</option>
    <option value="istirahat">istirahat</option>
    <option value="tanpa istirahat">tanpa istirahat</option>
</select>
<input type="text" name="status_check" size="8" readonly="readonly" id="status_check" style="text-transform:uppercase" />
<br /><br />
Total: <input type="text" id="jam_total" type="text" name="jam_total" size="18" readonly="readonly">
<br /><br />
<script>
function Choose1(data) {
    document.getElementById("status_check").value = data.value;
}
</script>
&#13;
&#13;
&#13;

尝试在以下代码中检查jQuery函数:

else if (stats1 != 'TANPA ISTIRAHAT') {
    var diff = ((((dtEnd - dtStart)) / 1000) - 1);
}

var diff = (((dtEnd - dtStart)) / 1000) - 1)就是我的意思,代码不会像我想的那样完美地工作。我想要的是这样的:

如果var diff = ((dtEnd - dtStart)) / 1000;我希望此代码的结果减去一小时,请检查内部代码。

1 个答案:

答案 0 :(得分:0)

首先,有一件事是错误的,您需要将stats1设置为UpperCase,否则您无法比较正确。第二件事是你计算的时间超过秒,你应该减去60*60(3600到-1小时)。

function updateHours(){   
    var dtStart = new Date("7/20/2015 " + $time1.val());
    var dtEnd = new Date("7/20/2015 " + $time2.val());
        var stats1 = $("#status_check").val().toUpperCase(); 
// if you don't add toUpperCase, istirahat won't be equal to ISTIRAHAT so everytime..
// first if block will be executed
   if(stats1!='ISTIRAHAT'){
        var diff = ((dtEnd - dtStart)) / 1000;
         }
   else if(stats1!='TANPA ISTIRAHAT'){

        var diff = ((((dtEnd - dtStart)) / 1000) - 3600); // and you should minus 3600
         }
    var totalTime = 0;

    if (diff > 60*60*12) {
        totalTime = formatDate(60*60*12);
        ;
    } else {
        totalTime = formatDate(diff);
    }

    $diff.val(totalTime);

}

这是工作fiddle

希望有所帮助,