当前位置: 首页 > 图灵资讯 > 行业资讯> python怎么把时间转换为时间戳

python怎么把时间转换为时间戳

发布时间:2025-01-08 14:46:38

思路分析:

1、利用strptime()函数将时间转换成时间数组;

2、利用mktime()函数将时间数组转换成时间戳;

(推荐教程:Python入门教程)

Python time strptime() 函数根据指定的格式把一个时间字符串解析为时间元组。

语法:

time.strptime(string[,format])

参数:

string -- 时间字符串。

format -- 格式化字符串。

实现代码:

#coding:UTF-8
importtime

dt="2016-05-0520:28:54"

#转换成时间数组
timeArray=time.strptime(dt,"%Y-%m-%d%H:%M:%S")
#转换成时间戳
timestamp=time.mktime(timeArray)

print(timestamp)#1462451334.0

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09