Categories
Python Answers

How to calculate the date six months from the current date using the datetime Python module?

Spread the love

Sometimes, we want to calculate the date six months from the current date using the datetime Python module.

In this article, we’ll look at how to calculate the date six months from the current date using the datetime Python module.

How to calculate the date six months from the current date using the datetime Python module?

To calculate the date six months from the current date using the datetime Python module, we can use the relativetimedelta function.

For instance, we write:

from datetime import date
from dateutil.relativedelta import relativedelta

six_months = date(2020, 1, 1) + relativedelta(months=+6)
print(six_months)

We call date to create a date object.

Then we add 6 months to the date and return the new date with + relativedelta(months=+6).

And then we assign the sum to six_months.

Therefore, six_months is 2020-07-01.

Conclusion

To calculate the date six months from the current date using the datetime Python module, we can use the relativetimedelta function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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