belongs to collection: Python Date and Time Exercises
Given:
# 2020-02-25 given_date = datetime(2020, 2, 25).date()
Expected output:
2020-06-25
Solution:
dateutil
relativedelta
from datetime import datetime from dateutil.relativedelta import relativedelta # 2020-02-25 given_date = datetime(2020, 2, 25).date() months_to_add = 4 new_date = given_date + relativedelta(months=+ months_to_add) print(new_date)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Solution:
dateutil
module’srelativedelta
. We can add 4 months into the given date using arelativedelta
.relativedelta
is useful when we want to deal months with day 29, 30 31, It will properly adjust the days.