Loading

Python日期时间类型转换

一、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())

执行结果:

转换前: 1599210257.9613085
转换后: 2020-09-04 17:04:17.961308
转换前: 2020-09-04 17:04:17.961308
转换成时间戳: 1599210257.961308
转换成时间的元组: 1599210257.961308
posted @ 2020-12-11 20:33  猫鱼故巷  阅读(696)  评论(0编辑  收藏  举报