在Python中,如何将datetime对象从epoch(unix time)转换为毫秒? [英] How can I convert a datetime object to milliseconds since epoch (unix time) in Python?

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

问题描述

我有一个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.

我如何做?

推荐答案

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

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中,如何将datetime对象从epoch(unix time)转换为毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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