How to find subtimespan in a timespan like a string in python

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

我有一串这样的时间跨度:

timespan = '08.00-14:00'

我想知道这个时间跨度是否至少是n小时,比如2小时:

if timespan >= 2:
    do something
python timespan
1个回答
1
投票

编辑:你可以尝试类似的东西

from datetime import datetime, timedelta

timespan = '23.00-01:00'

start_time, end_time = timespan.split('-')
start_time = datetime.strptime(start_time, '%H.%M')
end_time = datetime.strptime(end_time, '%H:%M')

if end_time <= start_time:
    # end_time is on the next day, add 1 day to end_time
    end_time += timedelta(days=1)

duration = end_time - start_time

if duration >= timedelta(hours=2):
    print("The duration is at least 2 hours.")
else:
    print("The duration is less than 2 hours.")
© www.soinside.com 2019 - 2024. All rights reserved.