w3resource

Python: Add a month with a specified date

Python Datetime: Exercise-23 with Solution

Write a Python program to add a month with a specified date.

Sample Solution:

Python Code:

from datetime import date, timedelta
import calendar
start_date = date(2014, 12, 25)
days_in_month = calendar.monthrange(start_date.year, start_date.month)[1]
print(start_date + timedelta(days=days_in_month))

Sample Output:

2015-01-25

Flowchart:

Flowchart: Add a month with a specified date.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to get the number of days of a given month and year.
Next: Write a Python program to count the number of Monday of the 1st day of the month from 2015 to 2016.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()