查找下一个条件日期



让我们有两个变量:

  1. 开始日期
  2. days_of_months::list(示例:[1,2,10,31](

输出:日期>start_date,其("%d"(=列表成员之一。输出日期必须尽可能接近start_date。

示例

开始日期:2020-11-17

月的天数:[5,10,20]

输出:2020-11-20

开始日期:2020-11-17

月的天数:[5,10]

输出:2020-12-05

开始日期:2020-12-17

月的天数:[5,10]

输出:2021-01-05

开始日期:2021-02-17

月的天数:[5,10,31]

输出:2021-03-05

开始日期:2021-04-17

月的天数:[31]

输出:2021-05-31(2021年4月没有31天(

建议答案:

from calendar import monthrange
from datetime import datetime
from datetime import timedelta
def find_condition_date(start_date,days_month_list):
year = int(start_date.strftime("%Y"))
month = int(start_date.strftime("%m"))
day = int(start_date.strftime("%d"))

current_month_days = monthrange(year, month)[1]

if(day in days_month_list):
return start_date
else:
for day_of_month_selected in days_month_list:
if(day_of_month_selected>day and day_of_month_selected<=current_month_days):
add_days = day_of_month_selected - day
start_date = start_date + timedelta(days=add_days)
return start_date

selected_index = 0
total_days_selected = len(days_month_list)
if(month==12):
year+=1
month = 1
else:
month +=1
current_month_days = monthrange(year,month)[1]

while(True):
if(selected_index<total_days_selected):
first_day_of_month_selected = int(days_month_list[selected_index])
if(current_month_days>=first_day_of_month_selected):
return datetime(year,month,first_day_of_month_selected).date()
else:
selected_index = 0
if(month==12):
year+=1
month = 1
else:
month +=1
current_month_days = monthrange(year,month)[1]
else:
selected_index = 0
if(month==12):
year+=1
month = 1
else:
month +=1
current_month_days = monthrange(year,month)[1]
#Example 1          
start_date = datetime(2020,11,17)
days_month_list = [5,10,20]
print(find_condition_date(start_date,days_month_list))
#Example 2          
start_date = datetime(2020,11,17)
days_month_list = [5,10]
print(find_condition_date(start_date,days_month_list))
#Example 3          
start_date = datetime(2020,12,17)
days_month_list = [5,10]
print(find_condition_date(start_date,days_month_list))
#Example 4
start_date = datetime(2021,2,17)
days_month_list = [5,10,31]
print(find_condition_date(start_date,days_month_list))
#Example 5
start_date = datetime(2021,4,17)
days_month_list = [31]
print(find_condition_date(start_date,days_month_list))

上面的代码似乎按预期工作。

首先,从start_date中提取日期

然后对月天数进行排序,并在排序后的数组上迭代,以找到第一个大于开始日期的值

在可用日期内迭代,直到找到下一个最近的有效日期(根据需要递增月/年(:

from datetime import date

def valid_date(y, m, d):
try:
date(y, m, d)
return True
except ValueError:
return False

def find_next_date(start_date, days_of_month):
current_year, current_month, current_day = map(int, start_date.split('-'))
while True:
for day in days_of_month:
if day >= current_day and valid_date(current_year, current_month, day):
return f'{current_year}-{current_month:02}-{day:02}'
current_month += 1
current_day = 1
if current_month > 12:
current_month = 1
current_year += 1

assert find_next_date('2020-11-17', [5, 10, 20]) == '2020-11-20'
assert find_next_date('2020-11-17', [5, 10]) == '2020-12-05'
assert find_next_date('2020-12-17', [5, 10]) == '2021-01-05'
assert find_next_date('2021-02-17', [5, 10, 31]) == '2021-03-05'
assert find_next_date('2021-04-17', [31]) == '2021-05-31'

相关内容

  • 没有找到相关文章

最新更新