python 时间戳转为字符串时间 转为指定的显示样式

welcome to my blog

问题描述: 看不懂时间戳, 需要转成日常使用的时间格式(字符串时间)

解决方法

import time
#时间戳
t1 = 1336651555
#提取出时间戳中的年月日等时间信息
t1 = time.localtime(t1)
#按照指定的格式打印时间戳对应的时间
# %Y %m %d %H %M %S分别表示年月日时分秒
t1 = time.strftime("%Y-%m-%d %H:%M:%S", t1)
# 打印结果2012-05-10 20:05:55
print(t1)

其他相关操作: 获取当前时间戳, 将其转换为字符串时间

import time
# 获取当前时间戳
t1 = time.time()
#提取出时间戳中的年月日等时间信息
t1 = time.localtime(t1)
#按照指定的格式打印时间戳对应的时间
# 只打印年月日
t1 = time.strftime("%Y-%m-%d", t1)
# 打印结果 2020-01-13
print(t1)
发布了489 篇原创文章 · 获赞 101 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/littlehaes/article/details/103886498