How to convert ticks to datetime in Python?
I am trying to convert 52707330000 to 1 hour and 27 minutes and 50 seconds.
Somehow it works here - http://tickstodatetime.com/. I tried inspecting the element but I don't understand javascript.
The following will convert the ticks to a Python datetime object (from now) using datetime's timedelta.
import datetime
ticks = 52707330000
converted_ticks = datetime.datetime.now() + datetime.timedelta(microseconds = ticks/10)
Then something like:
converted_ticks.strftime("%Y-%m-%d %H:%M:%S") // '2015-08-07 14:17:48'
Hope this helps!
EDIT: Using just datetime.timedelta(microseconds = ticks/10) will give you the time, not relative to "now".
To get the same time as on the web-site:
#!/usr/bin/env python
from __future__ import division
from datetime import datetime, timedelta
ticks = 52707330000
dt = datetime(1, 1, 1) + timedelta(microseconds=ticks/10)
print(dt.isoformat() + "Z")
# -> 0001-01-01T01:27:50.733000Z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With