Write a Python program calculates the date six months from the current date using the datetime module


The program starts by importing the datetime module from the Python Standard Library. The datetime.date.today() method is called to get the current date as a datetime.date object. The datetime.timedelta function is called with an argument of 6 * 365 / 12 , which represents 6 months in terms of days. This creates a timedelta object representing 6 months.

The datetime.date object obtained from calling datetime.date.today() is then added to the timedelta object to get a new datetime.date object representing the date 6 months from the current date.

Finally, the isoformat method of the resulting datetime.date object is called, which returns the date in ISO format (YYYY-MM-DD) as a string. The string is then printed to the console using the print function. The result of this program is a string representation of the date that is 6 months from the current date, in ISO format.

Source Code

import datetime
print((datetime.date.today() + datetime.timedelta(6*365/12)).isoformat())

Output

2023-03-25

Example Programs