一、time转换为datetime
1)datetime.datetime.fromtimestamp(tm )
2)datetime.datetime.utcfromtimestamp(tm )
二、datetime->time
1)dt01.timestamp()
2)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())
执行结果:
C:\python\python.exeC:/python/demo/file3.py
转换前:1519892355.5060892
转换后:2018-03-01 16:19:15.506089
转换前:2018-03-01 16:19:15.506089
转换成时间戳:1519892355.506089
转换成时间的元组:1519892355.506089
Process finished with exit code 0