1

我无法让这个工作。我厌倦了寻找,但我什么也找不到。我没有约会,我只有时间(01:45、05:30)。

我只使用库“时间”,目前我有上面的代码,它给了我几个小时之间的分钟数。但这还不够。

在此示例中,最终结果应为:

225 分钟

3小时(四舍五入)

hour_beg = '01:45'
hour_close = '05:30'

hours_begin = int(hour_beg.split(':')[0]) # 01
minutes_begin = int(hour_beg.split(':')[1]) # 45
hours_end = int(hour_close.split(':')[0]) # 05
minutes_end = int(hour_close.split(':')[1]) # 30

total_minutes = abs((hours_begin - hours_end ) * 60)

我怎样才能做到这一点?

4

3 回答 3

4
>>> from dateutil import parser
>>> time1 = parser.parse("01:45")
>>> time2 = parser.parse("05:30")
>>> print (time2-time1).seconds
13500
>>> print (time2-time1).seconds/60
225
于 2013-09-12T17:17:42.353 回答
2

使用 datetime 获取 timedelta。

import datetime
hour_beg = '01:45'
hour_close = '05:30'

first = datetime.datetime.strptime(hour_beg, '%H:%M')
second = datetime.datetime.strptime(hour_close, '%H:%M')
delta = first - second
于 2013-09-12T17:16:16.957 回答
2

怎么样(hours_end - hours_begin) * 60 + minutes_end - minutes_begin

于 2013-09-12T17:14:14.083 回答