How to calculate the time interval between two time strings

前端 未结 12 2621
生来不讨喜
生来不讨喜 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:28

    Concise if you are just interested in the time elapsed that is under 24 hours. You can format the output as needed in the return statement :

    import datetime
    def elapsed_interval(start,end):
        elapsed = end - start
        min,secs=divmod(elapsed.days * 86400 + elapsed.seconds, 60)
        hour, minutes = divmod(min, 60)
        return '%.2d:%.2d:%.2d' % (hour,minutes,secs)
    
    if __name__ == '__main__':
        time_start=datetime.datetime.now()
        """ do your process """
        time_end=datetime.datetime.now()
        total_time=elapsed_interval(time_start,time_end)
    

提交回复
热议问题