[英]how to generate end date from start date with specific list
shift_lst = ["D","D","D","D","N","N","W","N","N","D","D","D","D","W","N","N","N"]
current_date = date(2021, 4, 1)
for shift in (shift_lst):
shift_dict=dict()
if shift == 'D':
shift_type = 'S1'
while shift_type == 'S1':
# start_date = current_date
# enddate = start_date + timedelta(days=count)
count = count + 1
elif shift == 'N':
shift='S2'
start_date = current_date
# enddate = start_date + timedelta(days=count)
count = count + 1
Output: Output:
{shift: S1,'start_date':2021,4,1,'end_date':2021,4,4},
{shift: S1,'start_date':2021,4,1,'end_date':2021,4,4},
{shift: S2,'start_date':2021,4,5,'end_date':2021,4,9},
{shift: S2,'start_date':2021,4,5,'end_date':2021,4,9},
{shift: S1,'start_date':2021,4,11,'end_date':2021,4,13}
{shift: S1,'start_date':2021,4,11,'end_date':2021,4,13}
I have list of Shift In that list i have D (Day shift) then date should start from 1st april to enddate until N not come then when N Start so start date also start from enddate of "D" and create dictionary of shift我有轮班列表 在该列表中我有 D(白班)然后日期应该从 4 月 1 日开始到结束日期直到 N 不来然后当 N 开始时所以开始日期也从“D”的结束日期开始并创建轮班字典
Hope this would be what you are looking for.希望这就是您要找的东西。 There is no need for
while
loop inside.里面不需要
while
循环。 You just increment your current_date
when it is D
or W
.您只需在
current_date
为D
或W
时增加它。
current_date = date(2021, 4, 1)
print("start date: ", current_date)
for shift in (shift_lst):
if shift == "D":
current_date = current_date + dt.timedelta(days=1)
elif shift == "W":
current_date = current_date + dt.timedelta(weeks=1)
elif shift == "N":
current_date = current_date
print(current_date)
end_date = current_date
print("end date: ", end_date)
output: output:
start date: 2021-04-01
2021-04-02
2021-04-03
2021-04-04
2021-04-05
2021-04-05
2021-04-05
2021-04-12
2021-04-12
2021-04-12
2021-04-13
2021-04-14
2021-04-15
2021-04-16
2021-04-23
2021-04-23
2021-04-23
2021-04-23
end date: 2021-04-23
simply what i want if shift == D then i want start date and end date of D, for example in first how many times D appear it 4 times so start date =2021-04-01 and end date = 2021-04-04 that's it same to N also from when N start and end i want just start date end date and create new dictionary for shift_type只是我想要的,如果 shift == D 那么我想要 D 的开始日期和结束日期,例如首先 D 出现了 4 次,所以开始日期 =2021-04-01 和结束日期 = 2021-04-04这对 N 也是一样的,从 N 开始和结束时我只想开始日期结束日期并为 shift_type 创建新字典
current_date = date(2021, 4, 1)
print("start date: ", current_date)
for shift in (shift_lst):
shift_dict=dict()
if shift == "D":
shift_type='S1'
current_date = current_date + dt.timedelta(days=1)
elif shift == "W":
current_date = current_date + dt.timedelta(weeks=1)
elif shift == "N":
shift_type='S2'
current_date = current_date
print(current_date)
end_date = current_date
shift_dict['shift_type']=shift
shift_dict['start_date']=current_date
shift_dict['end_date']=enddate
Output: Output:
{shift_type:S1,"start_date":"2021,4,1","end_date":"2021,4,4"} {shift_type:S2,"start_date":"2021,4,5","end_date":"2021,4,9"}
{shift_type:S1,"start_date":"2021,4,1","end_date":"2021,4,4"} {shift_type:S2,"start_date":"2021,4,5","end_date": “2021,4,9”}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.