2012-11-17 28 views
1

好的,有4個時鐘輸入/輸出實例。有一個時鐘,午餐時鐘,午餐時鐘,最後一個鐘錶。這是一個24小時制,時間需要四捨五入到最接近的0.25。(15分鐘)。需要幫助計算24小時內的4個實例時鐘之間的時間差

*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); 

}

的代碼是非常接近的工作,也有隻是隨機的情況下將無法正常工作,它會通過一個小時或一個小時左右.5關閉。另外,這是用javascript寫在託管於高度受限服務器上的HTML頁面上的,所以我無法真正添加任何新庫或任何東西。

如果你有更好的主意,肯定可以放棄我的代碼,這三個if/else語句有點混淆不清,這就是代碼無法正常工作的原因。我主要展示了代碼,以表明我一直在爲此付出一些努力,而不僅僅是試圖利用你的幫助哈哈。

我也很抱歉,但我不得不離開電腦一段時間,所以如果你有一些建議或方法來解決這個問題,請把它們扔出去,當我回來時我會看看它們。

+0

爲什麼要分別存儲分鐘和小時? – Blender

+0

這就是他們如何通過網站獲得輸入。一個領域的時間,另一個領域的分鐘。 – Hoser

回答

2

我們的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 

希望幫助你得到你的答案

0

的(相當)標準的方法來處理時間就是一切轉換成秒,並在數秒內完成數學。

時間1 =秒1 +(60 * minutes1)+(3600 *小時1)

等,如果時間可以跨午夜,你會希望他們參考了一些劃時代的時間(天文學家使用午夜1 2000年1月) 。然後減去你的時間,四捨五入到你需要的任何精度(15分鐘= 900秒),然後將你的三角洲轉換爲小時和分鐘。

相關問題