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

如何分辨两个datetime对象之间的时间差异?

 >>> 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中的timedeltatimedelta实例方法.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 

只要从另一个减去一个。 你得到一个有区别的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) 

您可以将dd.daysdd.secondsdd.microseconds转换为分钟。

使用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) 

如果ab是date时间对象,那么在Python 3中查找它们之间的时间差异:

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

在早期的Python版本上:

 time_difference_in_minutes = time_difference.total_seconds() / 60 

如果ab是天真的date时间对象,如datetime.now()返回,那么如果对象用不同的UTC偏移量表示本地时间,例如DST转换周围或过去/将来date,结果可能是错误的。 更多细节: 查找date时间之间是否经过了24小时 – Python 。

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

这是我如何获得两个datetime.datetime对象之间经过的小时数:

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

要find天数:timedelta有一个“天”的属性。 你可以简单地查询。

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

只是认为它可能是有用的提及formatdelta以及timedelta。 strptime()根据格式parsing表示时间的string。

 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) 

这将输出:0:05:00.518000

这是我使用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 

使用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 

持续几年

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

两个date之间的总持续时间

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