从Binance-API(Python)计算时间戳外的日期 [英] Compute Date out of Timestamp from Binance-API (Python)

查看:146
本文介绍了从Binance-API(Python)计算时间戳外的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Binance-API收到了服务器时间,我尝试使用它,它看起来像这样:

I received the servertime from the Binance-API,I try to work with and it looks like this:

{
  "serverTime": 1518440400000
}

问题是,如何计算该图章的日期?

The question is, how can I compute the date out of this stamp?

我尝试过

import datetime

print(datetime.datetime.fromtimestamp(
       int("1518308894652")).strftime('%Y-%m-%d %H:%M:%S'))

但是日期无效.

您有想法,还是具体?谢谢!

Do you have ideas, or is it to specific? Thank you!

推荐答案

您可以使用:

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

但是python表示年份超出范围(可以理解的是,考虑到它表示为50087).因此,我怀疑 serverTime 不是正常的时间戳.

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.

但是假设您得到的响应是时间戳,那么除了将字符串转换为int之外,您无需执行任何其他转换.

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.

原来文档说所有与时间和时间戳相关的字段都以毫秒为单位".因此,只需将响应除以1000,就可以了: datetime.fromtimestamp(int("1518308894652")/1000).来源

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

这篇关于从Binance-API(Python)计算时间戳外的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆