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

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

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

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

    New at Python 2.7 is the timedelta instance method .total_seconds(). From the Python docs, this is equivalent to (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6.

    Reference: http://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds

    >>> import datetime
    >>> time1 = datetime.datetime.now()
    >>> time2 = datetime.datetime.now() # waited a few minutes before pressing enter
    >>> elapsedTime = time2 - time1
    >>> elapsedTime
    datetime.timedelta(0, 125, 749430)
    >>> divmod(elapsedTime.total_seconds(), 60)
    (2.0, 5.749430000000004) # divmod returns quotient and remainder
    # 2 minutes, 5.74943 seconds
    
    0 讨论(0)
  • 2020-11-22 11:32

    To just find the number of days: timedelta has a 'days' attribute. You can simply query that.

    >>>from datetime import datetime, timedelta
    >>>d1 = datetime(2015, 9, 12, 13, 9, 45)
    >>>d2 = datetime(2015, 8, 29, 21, 10, 12)
    >>>d3 = d1- d2
    >>>print d3
    13 days, 15:59:33
    >>>print d3.days
    13
    
    0 讨论(0)
  • 2020-11-22 11:33

    Just subtract one from the other. You get a timedelta object with the difference.

    >>> import datetime
    >>> d1 = datetime.datetime.now()
    >>> d2 = datetime.datetime.now() # after a 5-second or so pause
    >>> d2 - d1
    datetime.timedelta(0, 5, 203000)
    

    You can convert dd.days, dd.seconds and dd.microseconds to minutes.

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

    I use somethign like this :

    from datetime import datetime
    
    def check_time_difference(t1: datetime, t2: datetime):
        t1_date = datetime(
            t1.year,
            t1.month,
            t1.day,
            t1.hour,
            t1.minute,
            t1.second)
    
        t2_date = datetime(
            t2.year,
            t2.month,
            t2.day,
            t2.hour,
            t2.minute,
            t2.second)
    
        t_elapsed = t1_date - t2_date
    
        return t_elapsed
    
    # usage 
    f = "%Y-%m-%d %H:%M:%S+01:00"
    t1 = datetime.strptime("2018-03-07 22:56:57+01:00", f)
    t2 = datetime.strptime("2018-03-07 22:48:05+01:00", f)
    elapsed_time = check_time_difference(t1, t2)
    
    print(elapsed_time)
    #return : 0:08:52
    
    0 讨论(0)
  • 2020-11-22 11:36

    You may find this fast snippet useful in not so much long time intervals:

        from datetime import datetime as dttm
        time_ago = dttm(2017, 3, 1, 1, 1, 1, 1348)
        delta = dttm.now() - time_ago
        days = delta.days # can be converted into years which complicates a bit…
        hours, minutes, seconds = map(int, delta.__format__('').split('.')[0].split(' ')[-1].split(':'))
    

    tested on Python v.3.8.6

    0 讨论(0)
  • 2020-11-22 11:37
    >>> import datetime
    >>> first_time = datetime.datetime.now()
    >>> later_time = datetime.datetime.now()
    >>> difference = later_time - first_time
    >>> seconds_in_day = 24 * 60 * 60
    datetime.timedelta(0, 8, 562000)
    >>> divmod(difference.days * seconds_in_day + difference.seconds, 60)
    (0, 8)      # 0 minutes, 8 seconds
    

    Subtracting the later time from the first time difference = later_time - first_time creates a datetime object that only holds the difference. In the example above it is 0 minutes, 8 seconds and 562000 microseconds.

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