問(wèn)題描述
我從 Binance-API 收到服務(wù)器時(shí)間,我嘗試使用它,它看起來(lái)像這樣:
I received the servertime from the Binance-API,I try to work with and it looks like this:
{
"serverTime": 1518440400000
}
問(wèn)題是,我怎樣才能從這張郵票中計(jì)算出日期?
The question is, how can I compute the date out of this stamp?
我試過(guò)了
import datetime
print(datetime.datetime.fromtimestamp(
int("1518308894652")).strftime('%Y-%m-%d %H:%M:%S'))
但日期無(wú)效.
你有什么想法,還是具體的?謝謝!
Do you have ideas, or is it to specific? Thank you!
推薦答案
你可以這樣用:
from datetime import datetime
datetime.fromtimestamp(int("1518308894652"))
但是 python 說(shuō)年份超出范圍(可以理解,考慮到它說(shuō)它是 50087).所以我懷疑 serverTime
不是正常的時(shí)間戳.
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.
但假設(shè)您得到的響應(yīng)是時(shí)間戳,所以除了將字符串轉(zhuǎn)換為 int 之外,您不需要進(jìn)行任何其他轉(zhuǎn)換.
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.
原來(lái)文檔說(shuō)所有時(shí)間和時(shí)間戳相關(guān)的字段都以毫秒為單位".所以只需將響應(yīng)除以 1000 就可以了:datetime.fromtimestamp(int("1518308894652")/1000)
.來(lái)源一個(gè)>
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
這篇關(guān)于從 Binance-API (Python) 計(jì)算時(shí)間戳之外的日期的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!