user11509999
user11509999

Reputation:

Convert epoch time to standard datetime from json python?

I have a JSON:

[{'id':'1',
'name':'rob',
'created':'1577431544986' 
    },

{'id':'2',
'name':'tom',
'created':'1566431234567' 
    }]

I need to convert the UNIX timestamp present in the JSON to standard DateTime.

My final output should be:

[{'id':'1',
'name':'rob',
'created':'27-12-2019' 
    },

{'id':'2',
'name':'tom',
'created':'22-08-2019' 
    }]

What i have done:

I'm able to figure out doing it for single variable:

import datetime
time = '1566431234567'
your_dt = datetime.datetime.fromtimestamp(int(time)/1000)
print(your_dt.strftime("%d-%m-%Y"))


'22-08-2019'

But how to use the same in above JSON?

Upvotes: 0

Views: 3267

Answers (1)

ikibir
ikibir

Reputation: 466

Epoch time is the number of seconds passed since January 1, 1970, 00:00:00 and you can convert it to human date time as

import time
# seconds passed since epoch
seconds = 1545925769.9618232
local_time = time.ctime(seconds)
print("Local time:", local_time)    

For more details check https://docs.python.org/3/library/time.html

Upvotes: 2

Related Questions