0

I was trying a simple date calculation by adding a certain number of days to a datetime.

import datetime
from dateutil.relativedelta import relativedelta
initial = datetime.date(2019, 3, 5)
delta = relativedelta(day=60)
print(f"Initial date: {initial.strftime('%d-%m-%Y')}")

new_dt = initial + delta
print(f"Final date: {new_dt.strftime('%d-%m-%Y')}")

However my output is:

Initial date: 05-03-2019
Final date: 31-03-2019

What is wrong here?

1 Answer 1

1
delta = relativedelta(day=60)
new_dt = initial + delta

The day of initial is set (not incremented but set) to 60 but since there are only 31 days in that month it is set to 31.

https://dateutil.readthedocs.io/en/stable/relativedelta.html

If your intention is to increment the date by 60 days use

delta = datetime.timedelta(days=60)

OR

delta = relativedelta(days=60)

instead of

delta = relativedelta(day=60)
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I could just have used relativedelta(days=60), days instead of day. That was my mistake. Thanks for pointing me to the docs.
@JoelGMathew added it to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.