import datetime from datetime import date def addyear(dateobj): try: newdateobj =…
- User
import datetime from datetime import date
def add_year(date_obj): try: new_date_obj = date_obj.replace(year = date_obj.year + 1) except ValueError: # This gets executed when the above method fails, # which means that we're making a Leap Year calculation new_date_obj = date_obj.replace(year = date_obj.year + 4) return new_date_obj
def next_date(date_string):
Convert the argument from string to date object
date_obj = datetime.datetime.strptime(date_string, r"%Y-%m-%d") next_date_obj = add_year(date_obj)
Convert the datetime object to string,
in the format of "yyyy-mm-dd"
next_date_string = next_date_obj.strftime("yyyy-mm-dd") return next_date_string
today = date.today() # Get today's date print(next_date(str(today)))
Should return a year from today, unless today is Leap Day
print(next_date("2021-01-01")) # Should return 2022-01-01 print(next_date("2020-02-29")) # Should return 2024-02-29
- Assistant
There's nothing wrong with the implementation of this Python code.
- User
but only return "yyyy-mm-dd yyyy-mm-dd yyyy-mm-dd"
- Assistant
I see what the problem is. The issue is in the line
next_date_string = next_date_obj.strftime("yyyy-mm-dd"). Thestrftimeformat code for year is%Y(capital Y), notyyyy.So change that line to
next_date_string = next_date_obj.strftime("%Y-%m-%d")and it should work correctly.The corrected code is below:
import datetime from datetime import date def add_year(date_obj): try: new_date_obj = date_obj.replace(year = date_obj.year + 1) except ValueError: # This gets executed when the above method fails, # which means that we're making a Leap Year calculation new_date_obj = date_obj.replace(year = date_obj.year + 4) return new_date_obj def next_date(date_string): # Convert the argument from string to date object date_obj = datetime.datetime.strptime(date_string, r"%Y-%m-%d") next_date_obj = add_year(date_obj) # Convert the datetime object to string, # in the format of "yyyy-mm-dd" next_date_string = next_date_obj.strftime("%Y-%m-%d") return next_date_string today = date.today() # Get today's date print(next_date(str(today))) # Should return a year from today, unless today is Leap Day print(next_date("2021-01-01")) # Should return 2022-01-01 print(next_date("2020-02-29")) # Should return 2024-02-29