How to increment a datetime by one day?


Question

How to increment the day of a datetime?

for i in range(1, 35)
    date = datetime.datetime(2003, 8, i)
    print(date)

But I need pass through months and years correctly? Any ideas?

1
115
11/5/2018 8:21:48 PM

Accepted Answer

date = datetime.datetime(2003,8,1,12,4,5)
for i in range(5): 
    date += datetime.timedelta(days=1)
    print(date) 
202
7/22/2013 10:27:26 PM

Incrementing dates can be accomplished using timedelta objects:

import datetime

datetime.datetime.now() + datetime.timedelta(days=1)

Look up timedelta objects in the Python docs: http://docs.python.org/library/datetime.html


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon