2014-12-25 66 views
0
from datetime import datetime 

FMT = '%H%M' 
rate1 = 35.34 
Midnight = "0000" 

signOnSun1 = raw_input("What time did you sign on Sunday: "); 
signOffSun1 = raw_input("What time did you sign off Sunday: "); 

totalShift = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (signOnSun1, FMT)) 

midnightToSignOff = (datetime.strptime(signOffSun1, FMT) - datetime.strptime (Midnight, FMT)) 
midnightToSignOff = diff.total_seconds()/60.0/60 

基本上這就是我所擁有的。如果我在1800年登錄並在0200晚上簽收,我無法返回8 8小時的正確答案如何計算第一天和第二天之間的時間(Python)

+0

你能給我們一個簡單的代碼表明你想要做什麼嗎?一個沒有大量不必要的常量和Tkinter和用戶輸入? –

+0

使用'datetime'(特定的'datetime.timedelta')來查找時間差異。 – alfasin

+0

好男人我轉發了謝謝 –

回答

0

問題是程序不知道班次在第二天結束。爲了解決這個問題,你可以檢查簽到時間是否晚於簽到時間,如果是這種情況,可以在簽到時間加一天。

from datetime import datetime, timedelta 

FMT = '%H%M' 
sign_on_time_str = raw_input("What time did you sign on Sunday: "); 
sign_off_time_str = raw_input("What time did you sign off Sunday: "); 
sign_on_time = datetime.strptime(sign_on_time_str, FMT) 
sign_off_time = datetime.strptime(sign_off_time_str, FMT) 

if sign_on_time > sign_off_time: 
    sign_off_time += timedelta(days=1) 

total_shift = sign_off_time - sign_on_time 
print total_shift.total_seconds()/60/60 
+0

THANKS BROOOOOOOOOOOOOO = D –