当前位置:首页 > Python教程 > python进阶

Python 将时间戳转换为指定格式日期

python 将时间戳转换为指定格式日期

python3 实例

给定一个时间戳,将其转换为指定格式的时间。

注意时区的设置。

当前时间

实例 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
指定时间戳

实例 3

import time timestamp = 1557502800 timearray = time.localtime(timestamp) otherstyletime = time.strftime("%y-%m-%d %h:%m:%s", timearray) print(otherstyletime)

执行以上代码输出结果为:

2019-05-10 23:40:00

实例 4

import datetime timestamp = 1557502800 datearray = datetime.datetime.utcfromtimestamp(timestamp) otherstyletime = datearray.strftime("%y-%m-%d %h:%m:%s") print(otherstyletime)

执行以上代码输出结果为:

2019-05-10 23:40:00

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!