Python: Converting from `datetime.datetime` to `time.time`

前端 未结 5 1862
醉话见心
醉话见心 2020-12-05 08:57

In Python, how do I convert a datetime.datetime into the kind of float that I would get from the time.time function?

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 09:52

    It's not hard to use the time tuple method and still retain the microseconds:

    >>> t = datetime.datetime.now()
    >>> t
    datetime.datetime(2011, 11, 5, 11, 26, 15, 37496)
    
    >>> time.mktime(t.timetuple()) + t.microsecond / 1E6
    1320517575.037496
    

提交回复
热议问题