How to calculate the time interval between two time strings

前端 未结 12 2454
生来不讨喜
生来不讨喜 2020-11-22 12:03

I have two times, a start and a stop time, in the format of 10:33:26 (HH:MM:SS). I need the difference between the two times. I\'ve been looking through documentation for

12条回答
  •  无人及你
    2020-11-22 12:42

    import datetime as dt
    from dateutil.relativedelta import relativedelta
    
    start = "09:35:23"
    end = "10:23:00"
    start_dt = dt.datetime.strptime(start, "%H:%M:%S")
    end_dt = dt.datetime.strptime(end, "%H:%M:%S")
    timedelta_obj = relativedelta(start_dt, end_dt)
    print(
        timedelta_obj.years,
        timedelta_obj.months,
        timedelta_obj.days,
        timedelta_obj.hours,
        timedelta_obj.minutes,
        timedelta_obj.seconds,
    )
    

    result: 0 0 0 0 -47 -37

提交回复
热议问题