Python 日期范围循环日期从 API
Python Date range looping date from API
要求:我必须将 start_date 和 end_date 传递给 API 并循环 b/w 日期范围
示例:日期范围是 start_date = '2022-05-23'
和 end_date ='2022-05-26'
所以当我从 API 循环数据时,它应该首先 start_date 和 end_date 如下
start_date = '2022-05-23' and end_date ='2022-05-24'
start_date = '2022-05-24' and end_date ='2022-05-25'
start_date = '2022-05-25' and end_date ='2022-05-26'
start_date = '2022-05-26' and end_date ='2022-05-27'
start_date = '2022-05-27' and end_date ='2022-05-28'
start_date = '2022-05-28' and end_date ='2022-05-29'
尝试如下,能够增加 start_date,不知道如何将 end_date 增加 1 天 start_date
from datetime import datetime, timedelta
start_date = "2022-05-24"
end_date = "2022-05-29"
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
delta = timedelta(days=1)
while start_dt <= end_dt:
print(f"start_dt:{start_dt}")
print(f"end_dt:{end_dt}")
start_dt += delta
[...] not sure how to increment end_date by 1 day of start_date
temp_dt = start_dt + datetime.timedelta(days=1)
print(f"start_dt:{start_dt}")
print(f"end_dt:{temp_dt}")
这应该没问题。
from datetime import datetime, timedelta
from datetime import date
start_date = "2022-05-24"
end_date = "2022-05-29"
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
gap=(end_dt-start_dt).days
print(gap)
end_dt=end_dt- timedelta(gap-1)
delta = timedelta(days=1)
for i in range(gap):
print(f"start_dt:{start_dt}")
print(f"end_dt:{end_dt}")
start_dt += delta
end_dt += delta
要求:我必须将 start_date 和 end_date 传递给 API 并循环 b/w 日期范围
示例:日期范围是 start_date = '2022-05-23'
和 end_date ='2022-05-26'
所以当我从 API 循环数据时,它应该首先 start_date 和 end_date 如下
start_date = '2022-05-23' and end_date ='2022-05-24'
start_date = '2022-05-24' and end_date ='2022-05-25'
start_date = '2022-05-25' and end_date ='2022-05-26'
start_date = '2022-05-26' and end_date ='2022-05-27'
start_date = '2022-05-27' and end_date ='2022-05-28'
start_date = '2022-05-28' and end_date ='2022-05-29'
尝试如下,能够增加 start_date,不知道如何将 end_date 增加 1 天 start_date
from datetime import datetime, timedelta
start_date = "2022-05-24"
end_date = "2022-05-29"
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
delta = timedelta(days=1)
while start_dt <= end_dt:
print(f"start_dt:{start_dt}")
print(f"end_dt:{end_dt}")
start_dt += delta
[...] not sure how to increment end_date by 1 day of start_date
temp_dt = start_dt + datetime.timedelta(days=1)
print(f"start_dt:{start_dt}")
print(f"end_dt:{temp_dt}")
这应该没问题。
from datetime import datetime, timedelta
from datetime import date
start_date = "2022-05-24"
end_date = "2022-05-29"
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
gap=(end_dt-start_dt).days
print(gap)
end_dt=end_dt- timedelta(gap-1)
delta = timedelta(days=1)
for i in range(gap):
print(f"start_dt:{start_dt}")
print(f"end_dt:{end_dt}")
start_dt += delta
end_dt += delta