1

好的,有 4 个发生时钟输入/输出的实例。有一个打卡,午餐打卡,午餐打卡,最后一个打卡。这是一个 24 小时制的时钟,最后需要将时间四舍五入到最接近的 0.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。此外,这是在高度受限的服务器上托管的 HTML 页面上用 javascript 编写的,所以我不能真正添加​​任何新库或任何东西。

如果您有更好的主意,绝对可以随意放弃我的代码,这三个 if/else 语句解释起来有点令人困惑,这就是为什么代码无论如何都不起作用的原因。我主要展示了代码以表明我一直在为此付出一些努力,而不仅仅是试图利用你的帮助哈哈。

我也很抱歉,但我必须离开电脑一段时间,所以如果你有一些解决这个问题的建议或方法,请把它们扔在那里,我回来时会看看它们。

4

2 回答 2

2

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

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

希望能帮助你得到你的答案

于 2012-11-17T02:52:40.240 回答
0

处理时间的(相当)标准方法是将所有内容转换为秒,并以秒为单位进行计算。

时间 1 = 秒 1 + (60 * 分钟 1) + (3600 * 小时 1)

等等。如果时间可以跨越午夜,你会想要将它们引用到某个纪元时间(天文学家使用 2000 年 1 月 1 日午夜)。

然后减去您的时间,四舍五入到您需要的任何精度(15 分钟 = 900 秒),然后将您的增量转换为小时和分钟。

于 2012-11-17T05:34:50.067 回答