Python日期时间类型转换
一、time转换为datetime
- datetime.datetime.fromtimestamp(tm )
- datetime.datetime.utcfromtimestamp(tm )
二、datetime->time
- dt01.timestamp()
- dt01.timetuple()
from time importtime
from datetime importdatetime
time01 = time()
print("转换前:", time01)
print("转换后:", datetime.fromtimestamp(time01))
dt01 = datetime.today()
print("转换前:", dt01)
print("转换成时间戳:", dt01.timestamp())
print("转换成时间的元组:", dt01.timestamp())
执行结果:
转换前: 1599210257.9613085
转换后: 2020-09-04 17:04:17.961308
转换前: 2020-09-04 17:04:17.961308
转换成时间戳: 1599210257.961308
转换成时间的元组: 1599210257.961308