Sil*_*ost 325

>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> c = b - a
datetime.timedelta(0, 8, 562000)
>>> divmod(c.days * 86400 + c.seconds, 60)
(0, 8)      # 0 minutes, 8 seconds
Run Code Online (Sandbox Code Playgroud)

  • 参考:http://docs.python.org/library/datetime.html#datetime-objects.阅读"支持的操作". (2认同)

Rya*_*ley 135

Python 2.7的新功能是timedelta实例方法.total_seconds().从Python文档中,这相当于(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6.

参考: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
Run Code Online (Sandbox Code Playgroud)

  • 你可以[使用`elapsedTime/timedelta(minutes = 1)`来获得Python 3中几分钟的差异](http://stackoverflow.com/a/26586909/4279) (3认同)

Att*_*que 65

使用datetime示例

>>> 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
Run Code Online (Sandbox Code Playgroud)

持续时间多年

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

持续时间(天)

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

持续时间(小时)

>>> hours = divmod(duration_in_s, 3600)[0]        # Seconds in an hour = 3600
Run Code Online (Sandbox Code Playgroud)

持续时间(分钟)

>>> minutes = divmod(duration_in_s, 60)[0]        # Seconds in a minute = 60
Run Code Online (Sandbox Code Playgroud)

持续时间(秒)

>>> seconds = duration.seconds                    # Build-in datetime function
>>> seconds = duration_in_s
Run Code Online (Sandbox Code Playgroud)

持续时间,以微秒为单位

>>> microseconds = duration.microseconds          # Build-in datetime function  
Run Code Online (Sandbox Code Playgroud)

两个日期之间的总持续时间

>>> 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]))
Run Code Online (Sandbox Code Playgroud)

或者干脆:

>>> print(now - then)
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么没有人感谢您。非常感谢您提供如此精确的答案。@阿塔克 (4认同)
  • 那是因为我无法计算一年中的秒数:) 365*24*60*60 = 31536000 而不是 31556926。我更新了答案和函数,它现在应该可以工作了。 (2认同)

Vin*_*jip 27

只需从另一个中减去一个.你得到一个timedelta与众不同的对象.

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

你可以转换dd.days,dd.secondsdd.microseconds以分钟.

  • 请注意:dd.seconds 并不表示两个日期时间之间的秒数。使用 dd.total_seconds() 代替。所以最后一行应该是 print (int(round(dd.total_seconds()/60, 0))) (3认同)
  • 这些参数是什么?评论会很好 (2认同)
  • @TheRealChx101 ```>>> dd = d2 - d1``` (2认同)

jfs*_*jfs 20

如果a,b是datetime对象然后在Python 3中找到它们之间的时差:

from datetime import timedelta

time_difference = a - b
time_difference_in_minutes = time_difference / timedelta(minutes=1)
Run Code Online (Sandbox Code Playgroud)

在早期的Python版本中:

time_difference_in_minutes = time_difference.total_seconds() / 60
Run Code Online (Sandbox Code Playgroud)

如果a,如果对象表示具有不同UTC偏移的本地时间,例如,围绕DST转换或过去/未来日期b,datetime.now()那么天真的日期时间对象(例如由当时返回的结果)可能是错误的.更多细节:查看日期时间之间是否已经过了24小时 - Python.

要获得可靠的结果,请使用UTC时间或时区感知日期时间对象.


tgw*_*ste 17

使用divmod:

now = int(time.time()) # epoch seconds
then = now - 90000 # some time in the past

d = divmod(now-then,86400)  # days
h = divmod(d[1],3600)  # hours
m = divmod(h[1],60)  # minutes
s = m[1]  # seconds

print '%d days, %d hours, %d minutes, %d seconds' % (d[0],h[0],m[0],s)
Run Code Online (Sandbox Code Playgroud)

  • 这应该放入 datetime 模块。我只是不明白为什么它只使用了几天、几秒和几毫秒…… (3认同)

spa*_*row 11

只是认为提及有关 timedelta 的格式可能会很有用。strptime() 根据格式解析表示时间的字符串。

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)
Run Code Online (Sandbox Code Playgroud)

这将输出:0:05:00.518000


Ton*_*ony 8

这是我获取两个datetime.datetime对象之间经过的小时数的方法:

before = datetime.datetime.now()
after  = datetime.datetime.now()
hours  = math.floor(((after - before).seconds) / 3600)
Run Code Online (Sandbox Code Playgroud)

  • 这不会起作用:`timedelta.seconds`只给出明确存储的秒数* - 文档保证总共少于一天.你想要`(after - before).total_seconds()`,它给出了跨越*whole*delta的秒数. (9认同)

Eyo*_*nyo 7

要获得hour,minutesecond, 你可以这样做

>>> import datetime
>>> first_time = datetime.datetime.now()
>>> later_time = datetime.datetime.now()
>>> difference = later_time - first_time
>>> m, s = divmod(difference.total_seconds(), 60)
>>> print("H:M:S is {}:{}:{}".format(m//60, m%60, s))
Run Code Online (Sandbox Code Playgroud)


小智 6

要查找天数:timedelta具有'days'属性.你可以简单地查询.

>>>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
Run Code Online (Sandbox Code Playgroud)