首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用if语句计算ot小时数

使用if语句计算ot小时数
EN

Stack Overflow用户
提问于 2017-05-08 15:10:49
回答 2查看 59关注 0票数 0

我想用javascript计算ot小时数,但这似乎是一段很长的代码。不是整个小时,到了半个小时就变得复杂了。到目前为止,这是我的代码。

代码语言:javascript
复制
<script>
$(function(){
    $('#intime,#outtime').on('input', function() {
        // declare variables
        var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
        var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

        var startTime = new moment("08:00:00", 'HH:mm:ss');
        var halfDay = new moment("12:30:00", 'HH:mm:ss');
        var wholeDay = new moment("18:00:00", 'HH:mm:ss');


        var othours = 0; // declare day as 0
        /*AFTER 18.00*/ 
        if (moment(intime).hour() == 8 && (moment(outtime).hour() == 18 && moment(outtime).minute() == 30)) {
            othours = 0.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 19 && moment(outtime).minute() == 30)) {
            othours = 1.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 20 && moment(outtime).minute() == 30)) {
            othours = 2.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 21 && moment(outtime).minute() == 30)) {
            othours = moment(outtime).hour() && moment(outtime).minute() - moment(wholeDay).hour() && moment(wholeDay).minute();
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 22 && moment(outtime).minute() == 30)) {
            othours = 4.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 23 && moment(outtime).minute() == 30)) {
            othours = 5.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
            othours = 6.5;
        }
        /*BEFORE 8.00*/
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
            othours = 6.5;
        }
        else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) {
            othours = 6.5;
        }

        /*WHOLE HOURS*/
        else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) {
            othours = moment(outtime).hour() - moment(wholeDay).hour();
        }
        else if (moment(intime).hour() < 8 && moment(outtime).hour() > 18) {
            othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour();
        }
        else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) {
            othours = moment(startTime).hour() - moment(intime).hour();
        }
      $("#othours").val(othours);

    });
})
</script>

有没有更短的方法,或者我还得像这样继续半个小时?在if intime= 6.30和outtime= 19.30中,其他时间应该是3小时。

这是我的输出。我想在进入进站和出站时间时计算加班时数

EN

回答 2

Stack Overflow用户

发布于 2017-05-08 17:03:51

试试这个-

代码语言:javascript
复制
<script>
$(function(){
    $('#intime,#outtime').on('input', function() {
        // declare variables
        var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
        var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

        var othours = 0; // declare day as 0

        othours = moment.utc(moment(outtime,"DD/MM/YYYY HH:mm:ss").diff(moment(intime,"DD/MM/YYYY HH:mm:ss"))).format("HH")
        if(othours>10)
            $("#othours").val(othours-10);
        else
            $("#othours").val("Whole Day not work");

    });
})
</script>
票数 0
EN

Stack Overflow用户

发布于 2017-05-08 17:11:42

这与我正在寻找的东西是一致的。

代码语言:javascript
复制
<script>
$(function(){
    $('#intime,#outtime').on('input', function() {
        // declare variables
        var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime
        var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

        var startTime = new moment("08:00:00", 'HH:mm:ss');
        var wholeDay = new moment("18:00:00", 'HH:mm:ss');

        var othours = 0; // declare othours as 0

        if (moment(intime).hour() == 8 && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) {
            othours = moment(outtime).hour() - moment(wholeDay).hour() + 0.5;
        }
        else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) {
            othours = moment(outtime).hour() - moment(wholeDay).hour();
        }
        else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) {
            othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour();
        }
        else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() == 18) {
            othours = moment(startTime).hour() - moment(intime).hour() - 0.5;
        }
        else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) {
            othours = moment(startTime).hour() - moment(intime).hour();
        }
        else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() > 18) {
            othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() - 0.5;
        }
        else if (moment(intime).hour() < 8 && (moment(outtime).hour() > 18) && moment(outtime).minute() == 30) {
            othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() + 0.5;
        }

        $("#othours").val(othours);

    });
})
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43841756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档