如何计算经过的时间 Python [关闭]

问题描述 投票:-1回答:1

如何在没有datetime函数的情况下用python计算经过的时间?在我的python版本中

a = '2200'
b = '1800'
time1 = datetime.strptime(a,"%H%M") # convert string to time
time2 = datetime.strptime(b,"%H%M") 
diff = time1 -time2
diff.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
python
1个回答
1
投票

像这样的东西可以计算出来

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)
© www.soinside.com 2019 - 2024. All rights reserved.