python时间戳与日期相互转换

import time

# 时间戳转日期
timeStamp = 1656399821  # 十位时间戳
timeArray = time.localtime(timeStamp)
printDate = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(printDate)  # 2022-06-28 15:03:41

# 日期转时间戳
date = "2022-06-28 10:20:59"
timeArray = time.strptime(date, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
print(timeStamp) # 1656382859

 

posted @ 2022-06-28 15:05  OTAKU_nicole  阅读(223)  评论(0编辑  收藏  举报