无法以日期格式转换 unix 时间戳
Unable to convert unix timestamp in date format
我正在尝试将 unix 时间戳转换为类似 DD/MM/YYYY HH:MM:SS 的日期。
由于某种原因,我无法正常工作。你能看看我做错了什么吗,我刚开始使用 Python 3.8.8
import requests, json
import datetime as dt
r = requests.get('https://api1.binance.com/api/v3/ticker/24hr')
jm = json.loads(r.text)
for n in range(len(jm)):
x=(jm[n] ['closeTime'])
# printing unix timestamp
print (x)
# trying to get the date time
time_val = dt.datetime.fromtimestamp(x)
print(time_val)
感谢您的宝贵时间和帮助。谢谢
你得到的时间戳是以毫秒为单位的,你需要把它转换成秒,比如
time_val = dt.datetime.fromtimestamp(x/1000)
那是因为 API 以毫秒为单位给出结果,
请尝试:
time_val = dt.datetime.fromtimestamp(x/1000.0)
你应该试试这个:
import requests, json
import datetime as dt
r = requests.get('https://api1.binance.com/api/v3/ticker/24hr')
jm = json.loads(r.text)
for n in range(len(jm)):
x=(jm[n] ['closeTime'])
# printing unix timestamp
print (x)
# trying to get the date time
time_val = dt.datetime.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S')
print(time_val)
最后几个输出:
1630932847657
2021-09-06 18:54:07
1630932864523
2021-09-06 18:54:24
1630932804755
2021-09-06 18:53:24
1630932871298
2021-09-06 18:54:31
1630932771147
2021-09-06 18:52:51
1630932862321
2021-09-06 18:54:22
1630932863872
2021-09-06 18:54:23
1630932871522
2021-09-06 18:54:31
1630932870246
2021-09-06 18:54:30
1630932872709
2021-09-06 18:54:32
1630932872684
2021-09-06 18:54:32
1630932870210
2021-09-06 18:54:30
1630932872655
2021-09-06 18:54:32
1630932800127
2021-09-06 18:53:20
我正在尝试将 unix 时间戳转换为类似 DD/MM/YYYY HH:MM:SS 的日期。 由于某种原因,我无法正常工作。你能看看我做错了什么吗,我刚开始使用 Python 3.8.8
import requests, json
import datetime as dt
r = requests.get('https://api1.binance.com/api/v3/ticker/24hr')
jm = json.loads(r.text)
for n in range(len(jm)):
x=(jm[n] ['closeTime'])
# printing unix timestamp
print (x)
# trying to get the date time
time_val = dt.datetime.fromtimestamp(x)
print(time_val)
感谢您的宝贵时间和帮助。谢谢
你得到的时间戳是以毫秒为单位的,你需要把它转换成秒,比如
time_val = dt.datetime.fromtimestamp(x/1000)
那是因为 API 以毫秒为单位给出结果, 请尝试:
time_val = dt.datetime.fromtimestamp(x/1000.0)
你应该试试这个:
import requests, json
import datetime as dt
r = requests.get('https://api1.binance.com/api/v3/ticker/24hr')
jm = json.loads(r.text)
for n in range(len(jm)):
x=(jm[n] ['closeTime'])
# printing unix timestamp
print (x)
# trying to get the date time
time_val = dt.datetime.fromtimestamp(x/1000).strftime('%Y-%m-%d %H:%M:%S')
print(time_val)
最后几个输出:
1630932847657
2021-09-06 18:54:07
1630932864523
2021-09-06 18:54:24
1630932804755
2021-09-06 18:53:24
1630932871298
2021-09-06 18:54:31
1630932771147
2021-09-06 18:52:51
1630932862321
2021-09-06 18:54:22
1630932863872
2021-09-06 18:54:23
1630932871522
2021-09-06 18:54:31
1630932870246
2021-09-06 18:54:30
1630932872709
2021-09-06 18:54:32
1630932872684
2021-09-06 18:54:32
1630932870210
2021-09-06 18:54:30
1630932872655
2021-09-06 18:54:32
1630932800127
2021-09-06 18:53:20