导入模块
import datetime
获取当前的日期时间,返回datetime对象
datetime对象 = datetime.datetime.now()
datetime对象的属性
datetime对象.year
datetime对象.month
datetime对象.day
datetime对象.hour
datetime对象.minute
datetime对象.second
将时间戳转为datetime对象
datetime对象 = datetime.datetime.fromtimestamp(时间戳)
时间的增量对象
时间增量对象 = datetime.timedelta(时间单位=值)
d = datetime.timedelta(days=1)
datetime.timedelta(minutes=5)
datetime.timedelta(seconds=300)
datetime.timedelta(hours=1)
import datetime
d1 = datetime.datetime.now()
print(d1)
d = datetime.timedelta(days=1)
print(d)
d1 += d
print(d1)
手动生成时间对象
datetime.date(年,月,日)
datetime.time(时,分,秒)
datetime.datetime(年,月,日,时,分,秒)
import datetime
d = datetime.time(20,1,1)
print(d)
d1 = datetime.date(2018,1,1)
print(d1)
d2 = datetime.datetime(2018,1,1,1,2,20)
print(d2)
日期对象转为时间戳
时间戳 = 日期对象.timestamp()
import datetime
d = datetime.datetime(2018, 2, 3)
print(d)
stamp = d.timestamp()
print(stamp)