I need to increment the month of a datetime value

next_month = datetime.datetime(mydate.year, mydate.month+1, 1)

when the month is 12, it becomes 13 and raises error “month must be in 1..12”. (I expected the year would increment)

I wanted to use timedelta, but it doesn’t take month argument.
There is relativedelta python package, but i don’t want to install it just only for this.
Also there is a solution using strtotime.

time = strtotime(str(mydate));
next_month = date("Y-m-d", strtotime("+1 month", time));

I don’t want to convert from datetime to str then to time, and then to datetime; therefore, it’s still a library too

Does anyone have any good and simple solution just like using timedelta?

21 Answers
21

Leave a Reply

Your email address will not be published. Required fields are marked *