好吧,有4个时钟输入/输出发生的实例。有一个打卡时间,一个午餐打卡时间,一个午餐打卡时间,最后一个打卡时间这是一个24小时制的钟,时间需要四舍五入到最接近的点。最后是25(15分钟)。

   *For reference, a is the first clock in hour, b is the clock out for lunch hour
    c is the clock in hour from lunch, and d is the final clock out hour

   *e is the first clock in minute, f is the clock out for lunch minute
    g is the clock in from lunch minute, and h is the final clock out minute

   *So for example, if somebody clocked in at 5:30, a=5 and f=30
    a clock out for lunch at 12:00 gives b=12 and g=0, etc.

   *The way this code works in a nutshell is to find the total time elapsed and then
    subtract the lunch break time from it.


   //Find total time
    a=d-a;
    e=h-e;

    //Find lunch break time
    y=c-b;
    z=g-f;

    if(e>=0 && z>=0){
        hours=a-y;
        minutes=e-z;
        if(minutes<0){minutes=minutes*(-1);}
    }else if(e<0 && z<0){
        e=e*(-1);
        z=z*(-1);
        hours=a-y;
        minutes=e-z;
        if(minutes<0){minutes=minutes*(-1);}
    }else{
        if(e<0){e=e*(-1);}
        if(z<0){z=z*(-1);}
        if(e<=z){hours=a-y-1;}
        else{hours=a-y;}
        minutes=e-z;
        if(minutes<0){minutes=minutes*(-1);}
    }

    a=hours;
    e=minutes;

    //This rounds to the nearest 15 minutes/quarter hour
    if(e<15){
        if(e>=8){e=15;}
        else if(e<8){e=0;}}
    if(e<=30 && e>=15){
        if(e>=23){e=30;}
        else if(e<23){e=15;}}
    if(e<=45 && e>30){
        if(e>=38){e=45;}
        else if(e<38){e=30;}}
    if(e<=60 && e>45){
        if(e>=53){e=0;}
        else if(e<53){e=45;}}

    e=e/60;
    a=a+e;
}

if(a<0){a=a+24;}

$(totaltime).attr('value',a);

}
这段代码非常接近于工作状态,只是在随机情况下它不会工作,并且会在一个小时或一个小时的0.5小时左右关闭。另外,这是用javascript在一个高度受限的服务器上托管的html页面上编写的,因此我不能真正添加任何新的库或任何东西。
如果您有更好的想法,可以随意废弃我的代码,这三个if/else语句解释起来有点混乱,这就是代码无论如何都不能工作的原因。我主要是展示了代码,以表明我已经付出了一些努力,这并不只是想利用你的帮助哈哈。
我也很抱歉,但我得离开电脑一段时间,所以如果你有什么建议或方法来解决这个问题,请把它们扔出去,我回来后再看一看。

最佳答案

让javascript使用date类完成所有工作:

//Assuming the day is today
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDay();
//

var clockIn = new Date(year, month, day, a, e).getTime();
var clockOut = new Date(year, month, day, b, f).getTime();
var clockIn_afterLunch = new Date(year, month, day, c, g).getTime();
var clockOut_afterLunch = new Date(year, month, day, d, h).getTime();

var preLunchTimeWorked = clockOut - clockIn;
var postLunchTimeWorked = clockOut_afterLunch - clockIn_afterLunch;

var timeWorked = preLunchTimeWorked + postLunchTimeWorked;

var secondsWorked = timeWorked/1000;
var minutesWorked = secondsWorked/60;
var hoursWorked = minutesWorked/60;
//Much easier to work with in my opinion

希望这能帮你找到答案

07-28 02:28
查看更多