Problem · Math
Days Between
Learn this problemProblem statement
Write a function daysBetween that returns the number of days between two calendar dates.
Each date is represented by three integers: year, month, and day. The first date is guaranteed to occur before the second date.
Do not use system-provided date objects or built-in date-difference helpers. Compute the answer directly from the date components.
Function
daysBetween(year1: int, month1: int, day1: int, year2: int, month2: int, day2: int) → intComplete the function daysBetween in the editor below.
daysBetween has the following parameters:
int year1: the year of the first dateint month1: the month of the first dateint day1: the day of the first dateint year2: the year of the second dateint month2: the month of the second dateint day2: the day of the second date
Returns
int: the number of days between the two dates
Examples
Example 1
year1 = 2010month1 = 5day1 = 1year2 = 2011month2 = 5day2 = 1return = 365From
2010-05-01 to 2011-05-01 there are 365 days.Example 2
year1 = 2020month1 = 2day1 = 27year2 = 2020month2 = 3day2 = 1return = 3The interval crosses leap day in
2020: Feb 27 -> Feb 28 -> Feb 29 -> Mar 1, so the difference is 3 days.Example 3
year1 = 2019month1 = 12day1 = 31year2 = 2020month2 = 1day2 = 1return = 1The dates are consecutive calendar days across a year boundary.
Constraints
1 ≤ month1, month2 ≤ 12- Both input dates are valid calendar dates.
- The first date always occurs before the second date.
- Do not use built-in date libraries or system date objects.