python如何转换时间戳


当前第2页 返回上一页

2.3 时间戳转换为指定格式的日期

1

2

3

4

5

6

7

8

9

10

# 使用time

timeStamp = 1381419600

timeArray = time.localtime(timeStamp)

otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)

print otherStyleTime   # 2013--10--10 23:40:00

# 使用datetime

timeStamp = 1381419600

dateArray = datetime.datetime.utcfromtimestamp(timeStamp)

otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")

print otherStyleTime   # 2013--10--10 15:40:00

2.4 获取当前时间并且用指定格式显示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# time获取当前时间戳

now = int(time.time())     # 1533952277

timeArray = time.localtime(now)

print timeArray

otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)

print otherStyleTime   

 

# 结果如下

time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)

2018--08--11 09:51:17

 

 

# datetime获取当前时间,数组格式

now = datetime.datetime.now()

print now

otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")

print otherStyleTime 

 

# 结果如下:

2018-08-11 09:51:17.362986

2018--08--11 09:51:17

相关教程推荐:Python视频教程

以上就是python如何转换时间戳的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

如何搭建Python环境

Python中tuple和list的区别

linux环境使用pdb调试Python的方法

如何使用vscode愉快的写Python于调试配置步骤_Python

怎么安装Python解释器

r语言和Python有必要都学吗

Python中的id()函数及读取list的方法介绍(代码示例)

Python如何安装whl文件

Python return用法是什么

实例讲解Python如何利用pandas查询数据

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...