0

In Python I have a list of dates as strings:

dates = ['2022-01-01', '2022-01-08', '2022-01-21']

I would like to increment these dates by one day and add them to this list, like so:

dates_new = ['2022-01-01', '2022-01-02', '2022-01-08', '2022-01-09', '2022-01-21', '2022-01-22']

What is the best way to achieve this?

1
  • 1
    These are strings, not dates. As you've probably already identified, you will need to parse these strings to create a datetime.datetime object, add a day, and then format them back to strings. What part of this problem are you stuck on? Each of these has individually already been asked and answered on Stack Overflow. Asking a specific question allows us to direct you to the answer to your actual question. Commented Jan 18, 2023 at 20:15

3 Answers 3

3

You will need to import a library:

import datetime

First convert the strings to dates using datetime.strptime. The important part of the code is the datetime.timedelta, is a function to sum days, month or years to a date. Create a new list to store the dates + 1, and then alternate the old date with the new date, that was calculated.

dates = ['2022-01-01', '2022-01-08', '2022-01-21']
newDates = []

for date in dates:
    tempDate = datetime.datetime.strptime(date, '%Y-%m-%d')
    tempDate = tempDate + datetime.timedelta(days=1)
    tempDate = tempDate.strftime('%Y-%m-%d')
    newDates.append(date)
    newDates.append(tempDate)

print(newDates)

Result:

['2022-01-01', '2022-01-02', '2022-01-08', '2022-01-09', '2022-01-21', '2022-01-22']
Sign up to request clarification or add additional context in comments.

2 Comments

OP wants the dates to be interleaved, so you might as well add them all to an output list in the for date in dates loop, as shown by Andrej
Sorry, didn't notice, I will fix it
2

Try:

from datetime import datetime, timedelta

one_day = timedelta(days=1)
dates = ["2022-01-01", "2022-01-08", "2022-01-21"]

out = []
for d in dates:
    x = datetime.strptime(d, "%Y-%m-%d")
    out.append(d)
    out.append((x + one_day).strftime("%Y-%m-%d"))

print(out)

Prints:

['2022-01-01', '2022-01-02', '2022-01-08', '2022-01-09', '2022-01-21', '2022-01-22']

1 Comment

Give some explanation about the code, or everyone will only copy the code and use it
1

With datetime.timedelta class to get the needed difference/offset in days:

from datetime import datetime, timedelta
from itertools import chain

dates = ['2022-01-01', '2022-01-08', '2022-01-21']
dates_inc = list(chain.from_iterable((d, str((timedelta(days=1) + datetime.strptime(d, "%Y-%m-%d")).date())) 
                                     for d in dates))
print(dates_inc)

['2022-01-01', '2022-01-02', '2022-01-08', '2022-01-09', '2022-01-21', '2022-01-22']

1 Comment

You aren't just using datetime.timedelta (which is not a function, btw). An explanation of all the other things you do (which make up most of the solution) would make your answer much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.