这篇教程Python 转换时间戳为指定格式日期写得很实用,希望能帮到您。 我们将会启用到time库:
当前时间
实例1:import time# 获得当前时间时间戳now = int(time.time())#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"timeArray = time.localtime(now)otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)print(otherStyleTime) 2019-05-21 18:02:49
实例2:import datetime# 获得当前时间now = datetime.datetime.now()# 转换为指定的格式otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")print(otherStyleTime) 2019-05-21 18:03:48
指定时间戳实例1:import timetimeStamp = 1557502800timeArray = time.localtime(timeStamp)otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)print(otherStyleTime) 2019-05-10 23:40:00
实例2:import datetimetimeStamp = 1557502800dateArray = datetime.datetime.utcfromtimestamp(timeStamp)otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")print(otherStyleTime)
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注51zixue.net的更多内容! 深入了解python的函数参数 Python进阶语法之类的继承 |