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


如何在没有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

解决方案:

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

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)

给TA打赏
共{{data.count}}人
人已打赏
解决方案

验证给定格式的字符串

2022-4-21 19:09:28

解决方案

产出与预期不符

2022-4-21 19:09:30

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索