Reputation: 477
I have a string 1615070997520
. This is a Unix timestamp, but this is for millisecond. When I convert this to date with converter, It gives me correct date (Saturday, March 6, 2021 10:49:57.520 PM GMT).
But with this code:
from datetime import datetime
ts = int("1615070997520")
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
It gives me an error which is ValueError: year 53149 is out of range
.
Is there any way to convert it into correct date like yyyy-mm-dd hh:mm:ss.ms
using Python?
Upvotes: 1
Views: 2031
Reputation: 6343
Try this one
ts = int("1615070997520")/1000
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
Upvotes: 2