Source From Here
Question
I have a Python datetime object that I want to convert to unix time, or seconds/milliseconds since the 1970 epoch.
How-To
It appears to me that the simplest way to do this is:
- test.py
Let's test it:
Question
I have a Python datetime object that I want to convert to unix time, or seconds/milliseconds since the 1970 epoch.
How-To
It appears to me that the simplest way to do this is:
- test.py
- from datetime import datetime
- import time
- epoch = datetime.datetime.utcfromtimestamp(0)
- def unix_time_millis(dt):
- return (dt - epoch).total_seconds() * 1000.0
- def unix_time_sec(dt):
- return (dt - epoch).total_seconds()
沒有留言:
張貼留言