Adding days to a date in Python using datetime and timedelta. Use Python Pandas to concatenate dates with strings.

I wanted to create 20 files with this syntax date-blog-post-name. I wrote a very extensive D3 Tutorial and wanted to break it down into smaller blog posts, published sequentially 5 days apart.

The blog posts needed to have this naming syntax:

I extracted the headlines H2 of the original blog post and made an array like this:

>>> posts = ['', '', 
'', '', 
'', '', '', 
'', '', '', 
'', '', '', '', '', 
'', '', 
'', '']

How do I add the prefix dates?


To have files like these?

Adding days to a date in Python

Let’s get to it.

>>> import datetime
>>> + 5


TypeError: unsupported operand type(s) for +: '' and 'int'

Should I then add the days to the day in the date?

>>> + 5

This can’t be right.

Adding days to date in Python with timedelta

Let’s look at the Python docs: datetime. Looking at timedelta

A timedelta object represents a duration, the difference between two dates or times.

You can do this:

new_date = old_date + datetime.timedelta(days=N)

With an example: