1

了解しました。クロックイン/クロックアウトが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);   

}

コードは動作に非常に近く、動作しない場合がランダムにあり、1時間または0.5時間ほどずれます。また、これは非常に制限されたサーバーでホストされているHTMLページのJavaScriptで記述されているため、新しいライブラリなどを実際に追加することはできません。

より良いアイデアがあれば、間違いなく私のコードをスクラップしてください。これらの3つの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 に答える