菜鳥在這裏,Python的轉換軍用時間用戶輸入和計算工作時間(datetime.timedelta)
我被困在試圖呈現用戶輸入軍用時間爲標準時間。代碼工作,到目前爲止,但我需要從結束時間在標準時間顯示減去12小時。我如何使用datetime.time來做到這一點?另外,我需要原始用戶輸入轉換爲整數來進行datetime.timedelta計算?前面的問題似乎並沒有回答我的代碼問題。
我的代碼是:
def timeconvert():
print "Hello and welcome to Python Payroll 1.0."
print ""
# User input for start time. Variable stored.
start = raw_input("Enter your check-in time in military format (0900): ")
# User input for end time. Variable stored.
end = raw_input("Enter your check-out time in military format (1700): ")
print ""
# ---------------------------------------------------------------------------
# Present user input in standard time format hhmm = hh:mm
# ---------------------------------------------------------------------------
import datetime, time
convert_start = datetime.time(hour=int(start[0:2]), minute=int(start[2:4]))
# need to find a way to subtract 12 from the hour to present end time in standard time
convert_end = datetime.time(hour=int(end[0:2]), minute=int(end[2:4]))
print 'You started at', convert_start.strftime("%H:%M"),'am', 'and ended at', convert_end.strftime("%H:%M"), 'pm'
# ---------------------------------------------------------------------------
# Use timedelta to caculate time worked.
# ---------------------------------------------------------------------------
# print datetime.timedelta
timeconvert()
raw_input("Press ENTER to exit program") # Closes program.
感謝。
啊哈,我明白注意#2的文檔中,現在我看到的格式作品的一個例子。我一定會使用錯誤檢查。非常感謝。 – Codesurfer 2013-04-21 13:56:18