easy +8 pts

Add Days to Date

Compute a new date by adding a given number of days to a start date.

Write a function `add_days(date_str, days)` that takes a string `date_str` in the format `YYYY-MM-DD` and an integer `days` (can be negative) and returns a string representing the date that is `days` days after the given date. The output must be in the same `YYYY-MM-DD` format. You may assume the input date is valid, but you should handle leap years correctly. Use the standard library's `datetime` module for date arithmetic.

Constraints

The date string is a valid calendar date between 0001-01-01 and 9999-12-31 inclusive. `days` is an integer between -1,000,000 and 1,000,000 inclusive.

Example

>>> add_days('2023-01-01', 1)
'2023-01-02'
>>> add_days('2020-02-28', 1)
'2020-02-29'
>>> add_days('2020-03-01', -1)
'2020-02-29'
>>> add_days('2023-12-31', 1)
'2024-01-01'
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Parse the date using `datetime.strptime` with the format `'%Y-%m-%d'`.
Use `timedelta(days=days)` to perform the arithmetic.
Format the result with `strftime('%Y-%m-%d')`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.