3 回答
TA贡献1898条经验 获得超8个赞
假设时间戳在字符串/整数列表中:
import datetime as dt
timestamp = [1545730073,1645733473] # or timestamp = ['1545730073','1645733473']
for ts in timestamp:
print(dt.datetime.fromtimestamp(int(ts)).date())
输出:
2018-12-25
2022-02-25
编辑:
使用列表理解:
print([dt.datetime.fromtimestamp(int(ts)).date() for ts in timestamp])
输出:
[datetime.date(2018, 12, 25), datetime.date(2022, 2, 25)]
编辑 2:
如果要替换时间戳,请使用enumerate():
import datetime as dt
timestamp = [1545730073,1645733473] # or timestamp = ['1545730073','1645733473']
for indx, ts in enumerate(timestamp):
timestamp[indx] = dt.datetime.fromtimestamp(int(ts)).date()
print(timestamp)
输出:
[datetime.date(2018, 12, 25), datetime.date(2022, 2, 25)]
TA贡献1840条经验 获得超5个赞
获取字符串日期'2018-12-25',像这样
dt_object.strftime("%Y-%m-%d")
如果你有时间戳列表,应该是这样的:
timestamp = [1545730073, 1545730073]
dates = [datetime.datetime.fromtimestamp(item).date() for item in timestamp]
添加回答
举报