Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Incrementing ISO date by 5 days

I am working with a date format that looks like this:

2023-04-25T16:00:00+00:00

I want to add 5 days to the current_date and return the same format as above

I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from datetime import datetime, timedelta

today = datetime.now()
iso_date = today.isoformat()
iso_date += timedelta(days=5)

which throws:

TypeError: can only concatenate str(not "datetime.timedelta") to str

>Solution :

The return value of isoformat() is a string. Your timedelta should be added to the Date object, not the string:

today = datetime.now()
today += timedelta(days=5)
iso_date = today.isoformat()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading