如何将日期时间字符串转换为毫秒 UNIX 时间戳?
pythonserver side programmingprogramming更新于 2025/7/4 4:28:17
您可以使用时间模块在 Python 中获取以毫秒为单位的当前时间。您可以使用 time.time 函数(作为浮点值)获取以秒为单位的时间。要将其转换为毫秒,您需要将其乘以 1000 并将其四舍五入。
示例
import time milliseconds = int(round(time.time() * 1000)) print(milliseconds)
输出
将给出输出 −
1514825676008
如果要将 datetime 对象转换为毫秒时间戳,可以使用 timestamp 函数,然后应用与上述相同的数学运算来获取毫秒时间。
示例
import time from datetime import datetime dt = datetime(2018, 1, 1) milliseconds = int(round(dt.timestamp() * 1000)) print(milliseconds)
输出
这将给出输出 −
1514745000000
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

