将 2 个给定日期之间的天数分成特定的批量大小并相应地开始和 end_date
Split days between 2 given dates into specific batch size and get the start and end_date accordingly
我想将 2 个给定日期之间的天数分成特定的批次大小,以使我的 api 调用更容易。
我目前正在使用它按月拆分。但是,需要根据用户输入的批量大小进一步拆分并开始 end_date.
例如:
start_date : 2020-01-01
end_date : 2020-01-31
batch: 10
输出:
start: 2020-01-01
end :2020-01-10
start : 2020-01-11
end: 2020-01-20
start : 2020-01-21
end: 2020-01-30
start: 2020-01-31
end: 2020-01-31
我这样做:
我应该做哪些改变?
from dateutil import rrule, parser
start = parser.parse('Jan 21 2020')
end = parser.parse('Oct 30 2020')
date_list = [start]
date_list.extend(list(rrule.rrule(rrule.MONTHLY, bymonthday=(-1,1), dtstart=start, until=end)))
date_list.append(end)
print(date_list)
您可以使用 datetime 和 timedelta 选项来获取批次。您不需要加载 dateutil 并进行复杂的操作。 datetime 已经能够像数字一样计算日期。使用 datetime 中的可用函数。
import datetime
start_date = '2020-01-01'
end_date = '2020-01-31'
batch = 10
#First convert the string version of start and end dates into datetime
start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
end = datetime.datetime.strptime(end_date, '%Y-%m-%d')
#then set the timedelta to the batch - 1 day
#end date is always calculated as 9 more days not 10 (hence -1)
step = datetime.timedelta(days=(batch-1))
#iterate through the loop until start <= end
while start <= end:
print ('Start :', start.date()) #print start date
start += step #add the timedelta to start
if start > end: start = end
print ('End :', start.date()) #print end date
start += datetime.timedelta(days=1) #now increment by 1 more to get start date
print ()
这个输出将是:
Start : 2020-01-01
End : 2020-01-10
Start : 2020-01-11
End : 2020-01-20
Start : 2020-01-21
End : 2020-01-30
Start : 2020-01-31
End : 2020-01-31
我想将 2 个给定日期之间的天数分成特定的批次大小,以使我的 api 调用更容易。 我目前正在使用它按月拆分。但是,需要根据用户输入的批量大小进一步拆分并开始 end_date.
例如:
start_date : 2020-01-01
end_date : 2020-01-31
batch: 10
输出:
start: 2020-01-01
end :2020-01-10
start : 2020-01-11
end: 2020-01-20
start : 2020-01-21
end: 2020-01-30
start: 2020-01-31
end: 2020-01-31
我这样做: 我应该做哪些改变?
from dateutil import rrule, parser
start = parser.parse('Jan 21 2020')
end = parser.parse('Oct 30 2020')
date_list = [start]
date_list.extend(list(rrule.rrule(rrule.MONTHLY, bymonthday=(-1,1), dtstart=start, until=end)))
date_list.append(end)
print(date_list)
您可以使用 datetime 和 timedelta 选项来获取批次。您不需要加载 dateutil 并进行复杂的操作。 datetime 已经能够像数字一样计算日期。使用 datetime 中的可用函数。
import datetime
start_date = '2020-01-01'
end_date = '2020-01-31'
batch = 10
#First convert the string version of start and end dates into datetime
start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
end = datetime.datetime.strptime(end_date, '%Y-%m-%d')
#then set the timedelta to the batch - 1 day
#end date is always calculated as 9 more days not 10 (hence -1)
step = datetime.timedelta(days=(batch-1))
#iterate through the loop until start <= end
while start <= end:
print ('Start :', start.date()) #print start date
start += step #add the timedelta to start
if start > end: start = end
print ('End :', start.date()) #print end date
start += datetime.timedelta(days=1) #now increment by 1 more to get start date
print ()
这个输出将是:
Start : 2020-01-01
End : 2020-01-10
Start : 2020-01-11
End : 2020-01-20
Start : 2020-01-21
End : 2020-01-30
Start : 2020-01-31
End : 2020-01-31