Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD
as shown in the examples.
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15
Constraints:
1971
and 2100
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the number of days between two dates using a brute force method, we can simply count each day individually. We start at the earlier date and increment one day at a time until we reach the later date. The total count of these increments gives us the number of days between the two dates.
Here's how the algorithm would work step-by-step:
import datetime
def number_of_days_between_two_dates_brute_force(date_one, date_two):
# Determine which date is earlier to start counting from
if date_one > date_two:
earlier_date = date_two
later_date = date_one
else:
earlier_date = date_one
later_date = date_two
day_count = 0
current_date = earlier_date
# Increment the date until it equals the later date
while current_date != later_date:
current_date += datetime.timedelta(days=1)
# Count the number of days we incremented
day_count += 1
return day_count
The best way to find the number of days between two dates is to convert each date into a single number representing the days since a fixed starting point. Once both dates are numbers, simply subtract them to find the difference.
Here's how the algorithm would work step-by-step:
def days_between_dates(year1, month1, day1, year2, month2, day2):
def is_leap_year(year):
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
def days_in_month(year, month):
if month == 2:
return 29 if is_leap_year(year) else 28
elif month in [4, 6, 9, 11]:
return 30
else:
return 31
def days_from_origin(year, month, day):
# We use year 0 as our fixed reference point.
total_days = 0
for current_year in range(0, year):
total_days += 366 if is_leap_year(current_year) else 365
# Add the days from the beginning of the year.
for current_month in range(1, month):
total_days += days_in_month(year, current_month)
total_days += day
return total_days
# Calculate the total number of days from year 0 for each date.
days1 = days_from_origin(year1, month1, day1)
days2 = days_from_origin(year2, month2, day2)
# Return the absolute difference between the two dates.
return abs(days1 - days2)
Case | How to Handle |
---|---|
Null or empty date strings | Throw an IllegalArgumentException or return a predefined error code to indicate invalid input. |
Identical dates | Return 0 as the number of days between the same date is zero. |
Invalid date format (e.g., incorrect separator, out-of-range values) | Use try-catch blocks or a dedicated validation function to check the date format and values, throwing an exception or returning an error if invalid. |
Date strings representing dates far in the past or future (potential integer overflow) | Use a 64-bit integer type or long type to store the number of days since an epoch to prevent potential overflow when calculating the difference. |
Leap year handling (February 29) | Account for leap years correctly in the day calculation by implementing or using a built-in leap year check. |
Dates spanning different centuries or millennia | The date calculation should handle different centuries correctly as the math needs to account for century leap year exceptions. |
Date1 is later than Date2 | Calculate the absolute difference to ensure a non-negative result, or explicitly handle the reverse order. |
Dates on the edge of integer limits (e.g., dates that, when converted to days since epoch, are close to MAX_INT or MIN_INT) | Ensure that internal calculations using integer representations of dates do not overflow by using appropriate data types (e.g., long) and considering intermediate value ranges. |