Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ticks to datetime in Python?

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.

like image 685
pr338 Avatar asked Oct 24 '25 18:10

pr338


2 Answers

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".

like image 168
Tyler Avatar answered Oct 26 '25 10:10

Tyler


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
like image 35
jfs Avatar answered Oct 26 '25 09:10

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!