How can I convert a datetime object to milliseconds since epoch (unix time) in Python?

前端 未结 13 1468
臣服心动
臣服心动 2020-11-22 07:49

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

How do I do this?

13条回答
  •  渐次进展
    2020-11-22 08:00

    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
    

提交回复
热议问题