Python 3 教程 Python 将字符串的时间转换为时间戳

tullio · 2022-03-15 13:58:31 · 热度: 240

给定一个字符串的时间,将其转换为时间戳。

实例

import time a1 = "2019-5-10 23:40:00" # 先转换为时间数组 timeArray = time.strptime(a1, "%Y-%m-%d %H:%M:%S") # 转换为时间戳 timeStamp = int(time.mktime(timeArray)) print(timeStamp) # 格式转换 - 转为 / a2 = "2019/5/10 23:40:00" # 先转换为时间数组,然后转换为其他格式 timeArray = time.strptime(a2, "%Y/%m/%d %H:%M:%S") otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray) print(otherStyleTime)

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

1557502800
2019/05/10 23:40:00

查看更多 Python 实例

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册