How to calculate the time interval between two time strings

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

    Yes, definitely datetime is what you need here. Specifically, the strptime function, which parses a string into a time object.

    from datetime import datetime
    s1 = '10:33:26'
    s2 = '11:15:49' # for example
    FMT = '%H:%M:%S'
    tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
    

    That gets you a timedelta object that contains the difference between the two times. You can do whatever you want with that, e.g. converting it to seconds or adding it to another datetime.

    This will return a negative result if the end time is earlier than the start time, for example s1 = 12:00:00 and s2 = 05:00:00. If you want the code to assume the interval crosses midnight in this case (i.e. it should assume the end time is never earlier than the start time), you can add the following lines to the above code:

    if tdelta.days < 0:
        tdelta = timedelta(days=0,
                    seconds=tdelta.seconds, microseconds=tdelta.microseconds)
    

    (of course you need to include from datetime import timedelta somewhere). Thanks to J.F. Sebastian for pointing out this use case.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 12:31

    Structure that represent time difference in Python is called timedelta. If you have start_time and end_time as datetime types you can calculate the difference using - operator like:

    diff = end_time - start_time
    

    you should do this before converting to particualr string format (eg. before start_time.strftime(...)). In case you have already string representation you need to convert it back to time/datetime by using strptime method.

    0 讨论(0)
  • 2020-11-22 12:32

    Take a look at the datetime module and the timedelta objects. You should end up constructing a datetime object for the start and stop times, and when you subtract them, you get a timedelta.

    0 讨论(0)
  • 2020-11-22 12:33
        import datetime
        
        day = int(input("day[1,2,3,..31]: "))
        month = int(input("Month[1,2,3,...12]: "))
        year = int(input("year[0~2020]: "))
        start_date = datetime.date(year, month, day)
        
        day = int(input("day[1,2,3,..31]: "))
        month = int(input("Month[1,2,3,...12]: "))
        year = int(input("year[0~2020]: "))
        end_date = datetime.date(year, month, day)
        
        time_difference = end_date - start_date
        age = time_difference.days
        print("Total days: " + str(age))
    
    0 讨论(0)
  • 2020-11-22 12:36

    Try this -- it's efficient for timing short-term events. If something takes more than an hour, then the final display probably will want some friendly formatting.

    import time
    start = time.time()
    
    time.sleep(10)  # or do something more productive
    
    done = time.time()
    elapsed = done - start
    print(elapsed)
    

    The time difference is returned as the number of elapsed seconds.

    0 讨论(0)
提交回复
热议问题