如何在 Python 中将日期时间对象转换为自纪元(unix 时间)以来的毫秒数? [英] How can I convert a datetime object to milliseconds since epoch (unix time) in Python?

查看:27
本文介绍了如何在 Python 中将日期时间对象转换为自纪元(unix 时间)以来的毫秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python datetime 对象,我想将其转换为 Unix 时间,或自 1970 年以来的秒/毫秒.

I have a Python datetime object that I want to convert to unix time, or seconds/milliseconds since the 1970 epoch.

我该怎么做?

推荐答案

在我看来,最简单的方法是

It appears to me that the simplest way to do this is

import datetime

epoch = datetime.datetime.utcfromtimestamp(0)

def unix_time_millis(dt):
    return (dt - epoch).total_seconds() * 1000.0

这篇关于如何在 Python 中将日期时间对象转换为自纪元(unix 时间)以来的毫秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆