如果沒有 datetime 函數,如何使用 python 計算經過的時間?在我的python版本上a = '2200'b = '1800'time1 = datetime.strptime(a,"%H%M") # convert string to timetime2 = datetime.strptime(b,"%H%M") diff = time1 -time2diff.total_seconds()/3600 # seconds to hour不起作用。也沒有# Create datetime objects for each time (a and b)dateTimeA = datetime.datetime.combine(datetime.date.today(), a)dateTimeB = datetime.datetime.combine(datetime.date.today(), b)# Get the difference between datetimes (as timedelta)dateTimeDifference = dateTimeA - dateTimeB# Divide difference in seconds by number of seconds in hour (3600) dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600
1 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
像這樣的東西可以計算出來
a = '2200'
b = '1800'
to_minutes = lambda x: (float(x[:2]) * 60) + float(x[2:])
to_hours = lambda x: float(x) / 60
minutes_elapsed = to_minutes(a) - to_minutes(b)
hours_elapsed = to_hours(minutes_elapsed)
添加回答
舉報
0/150
提交
取消