Fix the days between two dates helper

Review the AI's codeEasy12 min

Your task

Your team needed the number of days between two dates. The first draft returned 9 for 2026-01-01 to 2026-01-10, and that one passing check nearly shipped it. Your rule now: date code is not done until it handles reversed dates, leap years and bad input.

An assistant wrote days_between(start_date, end_date). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • start_date, end_date: dates as text.
  • Return the number of days from start_date to end_date as an integer, or -999999 when a date is invalid.

Rules the tests check:

  • A date is valid only in the exact form YYYY-MM-DD: 10 characters, - as the 5th and 8th, digits 0 to 9 everywhere else.
  • The month must be 1 to 12, and the day must exist in that month. February has 29 days in a leap year and 28 otherwise.
  • A leap year is divisible by 4, except a year divisible by 100, which is a leap year only when it is also divisible by 400. So 2000 is a leap year and 2100 is not.
  • If either date is invalid, return -999999.
  • Otherwise return the end date's day count minus the start date's: 0 for the same date, negative when end_date is earlier. Use these calendar rules for every year from 0000 to 9999.

Examples

days_between(start_date: str, end_date: str) -> int
Example 1
Inputdays_between("2026-01-01", "2026-01-10")
Output9
Example 2
Inputdays_between("2026-03-15", "2026-03-15")
Output0