Converting unix timestamp string to readable date

前端 未结 15 2467
别跟我提以往
别跟我提以往 2020-11-22 04:30

I have a string representing a unix timestamp (i.e. \"1284101485\") in Python, and I\'d like to convert it to a readable date. When I use time.strftime, I get a

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 05:11

    Use datetime module:

    from datetime import datetime
    ts = int("1284101485")
    
    # if you encounter a "year is out of range" error the timestamp
    # may be in milliseconds, try `ts /= 1000` in that case
    print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
    

提交回复
热议问题