Home > Net >  how to generate end date from start date with specific list
how to generate end date from start date with specific list

Time:12-12

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 :

{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 : 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

CodePudding user response:

Hope this would be what you are looking for. There is no need for while loop inside. You just increment your current_date when it is D or 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:

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

CodePudding user response:

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

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 :

{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"}

  • Related