easy +10 pts

Days Between Dates

Compute the number of days between two calendar dates.

Write a function `days_between(d1, d2)` that takes two date strings in the format `YYYY-MM-DD` and returns the absolute number of days between them, as an integer. The dates are valid calendar dates (with leap years considered). The order of the inputs does not matter; the result is always non-negative. For example, `days_between('2020-01-01', '2020-01-02')` returns `1`, and `days_between('2020-01-02', '2020-01-01')` also returns `1`. You may use Python's `datetime` module. Do not read from input or print anything.

Constraints

The dates are given as strings in the format `YYYY-MM-DD`. Years are between 1000 and 9999 inclusive. The function must return an integer. Time complexity: O(1) if using `datetime`.

Example

>>> days_between('2020-01-01', '2020-01-01')
0
>>> days_between('2020-01-01', '2020-01-02')
1
>>> days_between('2021-01-01', '2020-12-31')
1
>>> days_between('2020-02-28', '2020-03-01')
2
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Parse the strings into date objects using `datetime.strptime` or `date.fromisoformat`.
Subtract the two date objects to get a `timedelta`.
Use `abs()` on the `.days` attribute of the timedelta to get a non-negative integer.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.