How do I find the time difference between two datetime objects in python?

前端 未结 17 1008
無奈伤痛
無奈伤痛 2020-11-22 11:06

How do I tell the time difference in minutes between two datetime objects?

相关标签:
17条回答
  • 2020-11-22 11:41

    If a, b are datetime objects then to find the time difference between them in Python 3:

    from datetime import timedelta
    
    time_difference = a - b
    time_difference_in_minutes = time_difference / timedelta(minutes=1)
    

    On earlier Python versions:

    time_difference_in_minutes = time_difference.total_seconds() / 60
    

    If a, b are naive datetime objects such as returned by datetime.now() then the result may be wrong if the objects represent local time with different UTC offsets e.g., around DST transitions or for past/future dates. More details: Find if 24 hrs have passed between datetimes - Python.

    To get reliable results, use UTC time or timezone-aware datetime objects.

    0 讨论(0)
  • 2020-11-22 11:41

    This is my approach using mktime.

    from datetime import datetime, timedelta
    from time import mktime
    
    yesterday = datetime.now() - timedelta(days=1)
    today = datetime.now()
    
    difference_in_seconds = abs(mktime(yesterday.timetuple()) - mktime(today.timetuple()))
    difference_in_minutes = difference_in_seconds / 60
    
    0 讨论(0)
  • 2020-11-22 11:41

    I have used time differences for continuous integration tests to check and improve my functions. Here is simple code if somebody need it

    from datetime import datetime
    
    class TimeLogger:
        time_cursor = None
    
        def pin_time(self):
            global time_cursor
            time_cursor = datetime.now()
    
        def log(self, text=None) -> float:
            global time_cursor
    
            if not time_cursor:
                time_cursor = datetime.now()
    
            now = datetime.now()
            t_delta = now - time_cursor
    
            seconds = t_delta.total_seconds()
    
            result = str(now) + ' tl -----------> %.5f' % seconds
            if text:
                result += "   " + text
            print(result)
    
            self.pin_time()
    
            return seconds
    
    
    time_logger = TimeLogger()
    

    Using:

    from .tests_time_logger import time_logger
    class Tests(TestCase):
        def test_workflow(self):
        time_logger.pin_time()
    
        ... my functions here ...
    
        time_logger.log()
    
        ... other function(s) ...
    
        time_logger.log(text='Tests finished')
    

    and i have something like that in log output

    2019-12-20 17:19:23.635297 tl -----------> 0.00007
    2019-12-20 17:19:28.147656 tl -----------> 4.51234   Tests finished
    
    0 讨论(0)
  • 2020-11-22 11:43

    Using datetime example

    >>> from datetime import datetime
    >>> then = datetime(2012, 3, 5, 23, 8, 15)        # Random date in the past
    >>> now  = datetime.now()                         # Now
    >>> duration = now - then                         # For build-in functions
    >>> duration_in_s = duration.total_seconds()      # Total number of seconds between dates
    

    Duration in years

    >>> years = divmod(duration_in_s, 31536000)[0]    # Seconds in a year=365*24*60*60 = 31536000.
    

    Duration in days

    >>> days  = duration.days                         # Build-in datetime function
    >>> days  = divmod(duration_in_s, 86400)[0]       # Seconds in a day = 86400
    

    Duration in hours

    >>> hours = divmod(duration_in_s, 3600)[0]        # Seconds in an hour = 3600
    

    Duration in minutes

    >>> minutes = divmod(duration_in_s, 60)[0]        # Seconds in a minute = 60
    

    Duration in seconds

    [!] See warning about using duration in seconds in the bottom of this post

    >>> seconds = duration.seconds                    # Build-in datetime function
    >>> seconds = duration_in_s
    

    Duration in microseconds

    [!] See warning about using duration in microseconds in the bottom of this post

    >>> microseconds = duration.microseconds          # Build-in datetime function
    

    Total duration between the two dates

    >>> days    = divmod(duration_in_s, 86400)        # Get days (without [0]!)
    >>> hours   = divmod(days[1], 3600)               # Use remainder of days to calc hours
    >>> minutes = divmod(hours[1], 60)                # Use remainder of hours to calc minutes
    >>> seconds = divmod(minutes[1], 1)               # Use remainder of minutes to calc seconds
    >>> print("Time between dates: %d days, %d hours, %d minutes and %d seconds" % (days[0], hours[0], minutes[0], seconds[0]))
    

    or simply:

    >>> print(now - then)
    

    Edit 2019 Since this answer has gained traction, I'll add a function, which might simplify the usage for some

    from datetime import datetime
    
    def getDuration(then, now = datetime.now(), interval = "default"):
    
        # Returns a duration as specified by variable interval
        # Functions, except totalDuration, returns [quotient, remainder]
    
        duration = now - then # For build-in functions
        duration_in_s = duration.total_seconds() 
        
        def years():
          return divmod(duration_in_s, 31536000) # Seconds in a year=31536000.
    
        def days(seconds = None):
          return divmod(seconds if seconds != None else duration_in_s, 86400) # Seconds in a day = 86400
    
        def hours(seconds = None):
          return divmod(seconds if seconds != None else duration_in_s, 3600) # Seconds in an hour = 3600
    
        def minutes(seconds = None):
          return divmod(seconds if seconds != None else duration_in_s, 60) # Seconds in a minute = 60
    
        def seconds(seconds = None):
          if seconds != None:
            return divmod(seconds, 1)   
          return duration_in_s
    
        def totalDuration():
            y = years()
            d = days(y[1]) # Use remainder to calculate next variable
            h = hours(d[1])
            m = minutes(h[1])
            s = seconds(m[1])
    
            return "Time between dates: {} years, {} days, {} hours, {} minutes and {} seconds".format(int(y[0]), int(d[0]), int(h[0]), int(m[0]), int(s[0]))
    
        return {
            'years': int(years()[0]),
            'days': int(days()[0]),
            'hours': int(hours()[0]),
            'minutes': int(minutes()[0]),
            'seconds': int(seconds()),
            'default': totalDuration()
        }[interval]
    
    # Example usage
    then = datetime(2012, 3, 5, 23, 8, 15)
    now = datetime.now()
    
    print(getDuration(then)) # E.g. Time between dates: 7 years, 208 days, 21 hours, 19 minutes and 15 seconds
    print(getDuration(then, now, 'years'))      # Prints duration in years
    print(getDuration(then, now, 'days'))       #                    days
    print(getDuration(then, now, 'hours'))      #                    hours
    print(getDuration(then, now, 'minutes'))    #                    minutes
    print(getDuration(then, now, 'seconds'))    #                    seconds
    

    Warning: Caveat about built-in .seconds and .microseconds
    datetime.seconds and datetime.microseconds are capped to [0,86400) and [0,10^6) respectively.

    They should be used carefully if timedelta is bigger than the max returned value.

    Examples:

    end is 1h and 200μs after start:

    >>> start = datetime(2020,12,31,22,0,0,500)
    >>> end = datetime(2020,12,31,23,0,0,700)
    >>> delta = end - start
    >>> delta.microseconds
    RESULT: 200
    EXPECTED: 3600000200
    

    end is 1d and 1h after start:

    >>> start = datetime(2020,12,30,22,0,0)
    >>> end = datetime(2020,12,31,23,0,0)
    >>> delta = end - start
    >>> delta.seconds
    RESULT: 3600
    EXPECTED: 90000
    
    0 讨论(0)
  • 2020-11-22 11:45

    Just thought it might be useful to mention formatting as well in regards to timedelta. strptime() parses a string representing a time according to a format.

    from datetime import datetime
    
    datetimeFormat = '%Y/%m/%d %H:%M:%S.%f'    
    time1 = '2016/03/16 10:01:28.585'
    time2 = '2016/03/16 09:56:28.067'  
    time_dif = datetime.strptime(time1, datetimeFormat) - datetime.strptime(time2,datetimeFormat)
    print(time_dif)
    

    This will output: 0:05:00.518000

    0 讨论(0)
  • 2020-11-22 11:47

    this is to find the difference between current time and 9.30 am

    t=datetime.now()-datetime.now().replace(hour=9,minute=30)
    
    0 讨论(0)
提交回复
热议问题