Skip to content Skip to sidebar Skip to footer

Converting 13-digit Unixtime In Ms To Timestamp In Python

I want to convert 13-digit Unix-time in milliseconds to the timestamp : '1523126888080'==>> %Y-%m-%d %H:%M:%S I have tried the following code from this link, But I think this

Solution 1:

You have a millisecond-precise timestamp so first divide it by 1000 then feed it to datetime.datetime.fromtimestamp() for local timezone (or pass datetime.tzinfo of the target timezone as a second argument) or datetime.datetime.utcfromtimestamp() for UTC. Finally, use datetime.datetime.strftime() to turn it into a string of your desired format.

import datetime

timestamp = "1523126888080"
your_dt = datetime.datetime.fromtimestamp(int(timestamp)/1000)  # using the local timezoneprint(your_dt.strftime("%Y-%m-%d %H:%M:%S"))  # 2018-04-07 20:48:08, YMMV

Solution 2:

Try dividing 1523126888080 by 1000 to get a valid timestamp. You should also use %d instead of %D as argument for strftime, i.e:

import time
ts = 1523126888080 / 1000print(time.strftime("%d %H:%M", time.localtime(ts)))
# 29 21:04

Post a Comment for "Converting 13-digit Unixtime In Ms To Timestamp In Python"