Compute Date Out Of Timestamp From Binance-API (Python)


Answer :

You could use this:



from datetime import datetime
datetime.fromtimestamp(int("1518308894652"))


But python says the year is out of range (understandably, considering it says it's 50087). So I suspect that serverTime is not a normal timestamp.



But assuming the response that you got was the timestamp, so you don't need to do any other conversions other than turning the string into an int.



Edit:



Turns out the docs say "All time and timestamp related fields are in milliseconds." So just divide the response by 1000 and you'll be fine: datetime.fromtimestamp(int("1518308894652")/1000). Source



Your response is in milliseconds when datetime.fromtimestamp requires seconds.



import datetime

print(datetime.datetime.fromtimestamp(1518308894652/1000))

# 2018-02-10 19:28:14.652000


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable