# 如何findpython中的两个date时间对象之间的时间差异？

### 10 Solutions collect form web for “如何findpython中的两个date时间对象之间的时间差异？”

` `>>> 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` `

Python 2.7中的`timedelta``timedelta`实例方法`.total_seconds()` 。 从Python文档中，这相当于`(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6`

` `>>> 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` `

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

` `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)` `

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

` `time_difference_in_minutes = time_difference.total_seconds() / 60` `

` `before = datetime.datetime.now() after = datetime.datetime.now() hours = math.floor(((after - before).seconds) / 3600)` `

` `>>>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` `

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

` `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` `

` `>>> 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` `

` `>>> years = divmod(duration_in_s, 31556926)[0] # Seconds in a year=31556926.` `

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

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

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

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

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

` `>>> 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]))` `

` `>>> print(now - then)` `
• 我可以在同一台计算机上安装Python 3.x和2.x吗？
• 我如何检查（在运行时）如果一个类是另一个类的子类？
• 生成所有可能的三字母string的最佳方法是什么？
• 列表理解中的双重迭代
• 寻找内置Python函数的源代码？
• 什么是标准的Python文档string格式？
• Python - 'ascii'编解码器不能解码字节
• Python的等价的setInterval（）？
• 如何在Python中使用networkx绘制有向图？
• 哪些PEP必须读取？
• multiprocessing.Pool：何时使用apply，apply_async或map？