How to convert epoch time to time

2040 views python
0

I have epoch time like this 1488445200000 and I need to convert it to datetime variable. I tried

from datetime import datetime
dt = datetime.fromtimestamp(1488445200000 // 3600)
dt

I got

datetime.datetime(1983, 2, 7, 10, 10)

but the result does not seem to be right (the right one should be March 2, 2017 10:00:00 AM GMT+01:00)... what shall I do?

answered question

Use datetime.fromtimestamp(1488445200000 / 1000)

2 Answers

10

Try This:

Use datetime module:

from datetime import datetime
ts = int(1488445200000//1000)
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

posted this
0

Your timestamp is in milliseconds and datetime expects seconds. Divide by 1000 and it works:

$ python3 
Python 3.7.0 (default, Jun 29 2018, 20:13:13) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1488445200000 // 1000)
>>> dt
datetime.datetime(2017, 3, 2, 10, 0)

posted this

Have an answer?

JD

Please login first before posting an answer.