Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I calculate the date Three months from a input date using python

I am using the datetime Python module. I am looking to calculate the date 3 months from the input date. Can you help me to get out of this issue.Thanks in advance

import datetime
today = "2022-02-24"

three = today + datetime.timedelta(30*3)
print (three)

Also I tried using "relativedelta"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t add a timedelta to a string, you need to add it to a datetime instance

Note that 90 days, isn’t really 3 months

from datetime import datetime, timedelta

today = "2022-02-24"
three = datetime.strptime(today, "%Y-%m-%d") + timedelta(30 * 3)
print(three)  # 2022-05-25 00:00:00

three = datetime.today() + timedelta(30 * 3)
print(three)  # 2022-05-24 21:32:35.048700
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading