一、日期时间计算

1)timedelta 两个datetime之差

次类中包含如下属性:

1 、days 天数

2、 microseconds 微秒 (>=0并且<1秒)

3 、seconds: 秒数(>=0并且<1天)

 

演示:

from datetime import datetime

 

dt01 = datetime(2012, 12, 12, 12, 12, 12, 121212)

dt02 = datetime(2013, 11, 13, 13, 13, 11, 131313)

print(dt01 - dt02)

print(abs((dt01 - dt02).days)) # 相差多少天

print(abs((dt01 - dt02).seconds)) # 相差多少秒

print(abs((dt01 - dt02).microseconds)) # 相差多少微秒

 

print(dt01.strftime("%Y-%m-%d %H:%M:%S."), dt01.microsecond,end="")

print(dt02.strftime("%Y-%m-%d %H:%M:%S."), dt02.microsecond,end="相差")

print(abs((dt01-dt02).days),"",abs((dt01-dt02).seconds),"",abs((dt01-dt02).microseconds),"微秒")