Python中怎么将时间戳转换为指定格式日期?

1 个回答|199 次阅读
Coco老师 - 公众号:量位学堂Qbit编程
擅长:编程教育
import time 
  
timeStamp = 1646926800 
timeArray = time.localtime(timeStamp) 
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S",timeArray)
print(otherStyleTime)

结果:

2022-03-10 23:40:00

第二种方式:

import datetime 
timeStamp = 1646926800 
dateArray = datetime.datetime.utcfromtimestamp(timeStamp) 
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)

结果:

2022-03-10 15:40:00