编程语言
首页 > 编程语言> > python 时间戳与日期之间的转换

python 时间戳与日期之间的转换

作者:互联网

import time , datetime

# 日期转换时间戳
date_time = '2019-07-03 18:27:05'
# 转换为时间数组
array = time.strptime(date_time, "%Y-%m-%d %H:%M:%S")
print(array)
print(array.tm_year)
print(array.tm_mon) # 7
print(array.tm_mday) # 3
print(array.tm_hour)
print(array.tm_min)
print(array.tm_sec) #5
timestamp = int(time.mktime(array))
print(timestamp)

# 更换显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", array)
print(otherStyleTime)


# 时间戳转换为指定格式日期
timeStamp = 1562149625
timenow = time.localtime(timeStamp)
print(timenow)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timenow)
print otherStyleTime
# 使用datetime
timeStamp = 1562149625
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime

标签:转换,python,otherStyleTime,datetime,日期,tm,time,print,array
来源: https://www.cnblogs.com/love-chen-forever/p/11128301.html