当前位置:Gxlcms > Python > Python——时间换算

Python——时间换算

时间:2021-07-01 10:21:17 帮助过:84人阅读

利用datetime模块进行不同时间单位的换算

from datetime import timedelta   
a = timedelta(days=2, hours=6)  
b = timedelta(hours=4.5)  
c = a + b  
print(c.days)  
print(c.seconds)  
print(c.seconds / 3600)

表示特定的日期和时间

from datetime import datetime  
a = datetime(2016, 8, 30)  
print(a + timedelta(days=10))  
b = datetime(2016, 9, 30)  
d = b - a  
print(d.days)  
  
now = datetime.today()  
print(now)  
print(now + timedelta(minutes=10))

datetime模块可以正确处理闰年

a = datetime(2012, 3, 1)  
b = datetime(2012, 2, 28)  
print((a-b).days)  
c = datetime(2013, 3, 1)  
d = datetime(2013, 2, 28)  
print((c-d).days)

处理更为复杂的日期问题,如处理时区,模糊时间范围,计算节日的日期等可以用dateutil模块

人气教程排行