I have the following string:
mytime = \"2009-03-08T00:27:31.807Z\"
How do I convert it to epoch in python?
I tried:
Code:
import datetime
epoch = datetime.datetime(1970, 1, 1)
mytime = "2009-03-08T00:27:31.807Z"
myformat = "%Y-%m-%dT%H:%M:%S.%fZ"
mydt = datetime.datetime.strptime(mytime, myformat)
val = (mydt - epoch).total_seconds()
print(val)
> 1236472051.81
repr(val)
> '1236472051.807'
Notes:
time.strptime(), the returned time.struct_time does not support sub-second precision.%f format is for microseconds. When parsing it need not be the full 6 digits.