简体   繁体   中英

How to convert millisecond Unix timestamp to readable date?

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?

Try this one

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM