Home > Blockchain >  From Timestamp obtain datetime object (Python)
From Timestamp obtain datetime object (Python)

Time:12-24

I'm trying to convert this timestamp time into date. I've tried several functions (pandas.Timestamp()) but unsuccessfully.

It seems that the function datetime.fromtimestamp() is not able to recognize because of the final zeros (3 zeros). If I delete manually these zeros I'm able to convert the timestamp correctly.

This below is the code:

from datetime import datetime


ts_0 = 1640225340000
out_0 = datetime.fromtimestamp(ts_0)  #Invalid Input

#Manually change timestamp -> last three zeros deleted
ts_1 = 1640225340
out_1 = datetime.fromtimestamp(ts_0) 
#>>> datetime.datetime(2021, 12, 23, 3, 9)

If I go to EpochConverter and I copy and paste ts_0 I will get the correct answer immediatly...

How can I solve this simple task?

CodePudding user response:

Maybe I'm missing something, but can't you just divide your original timestamp (with the 3 extra zeros) by 1000?

from datetime import datetime

ts_0 = 1640225340000
out_0 = datetime.fromtimestamp(ts_0/1000)
  • Related