3 回答
TA贡献1802条经验 获得超5个赞
你可以简单的cast(类型转换)的值timestart.timestamp()和 timeend.timestamp(),这是floats,对ints,即:
from datetime import datetime, timedelta
timestart = datetime.now() - timedelta(hours = 24)
timeend = datetime.now()
s = int(timestart.timestamp())
e = int(timeend.timestamp())
params = {'start_date':s, 'end_date':e}
print(params)
输出:
{'start_date': 1554121647, 'end_date': 1554208047}
TA贡献1886条经验 获得超2个赞
我通常time.mktime()用于将日期时间转换为纪元时间:
from datetime import datetime, timedelta
import time
timestart = datetime.now() - timedelta(hours = 24)
timeend = datetime.now()
params = {
'start_date': int(time.mktime(timestart.timetuple())),
'end_date': int(time.mktime(timeend.timetuple()))
}
# Output
{'start_date': 1554123099, 'end_date': 1554209499}
TA贡献1827条经验 获得超8个赞
Pedro 的另一种解决方案:
from datetime import datetime
from datetime import timedelta
timestart = (datetime.now() - timedelta(hours = 24)).strftime("%s")
timeend = datetime.now().strftime("%s")
params = {'start_date':timestart,
'end_date':timeend}
输出:
{'start_date': '1554124346', 'end_date': '1554210746'}
添加回答
举报