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

前端 未结 13 1491
臣服心动
臣服心动 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:04

    This is how I do it:

    from datetime import datetime
    from time import mktime
    
    dt = datetime.now()
    sec_since_epoch = mktime(dt.timetuple()) + dt.microsecond/1000000.0
    
    millis_since_epoch = sec_since_epoch * 1000
    

提交回复
热议问题