Leetcode Problem 1360. Number of Days Between Two Dates

1360. Number of Days Between Two Dates

Leetcode Solutions

Calculating Days Between Two Dates Without Using Built-in Libraries

  1. Define a function isLeapYear(year) to check if a given year is a leap year.
  2. Define an array monthDays containing the number of days in each month for non-leap years.
  3. Define a function totalDaysToDate(year, month, day) that calculates the total number of days from year 0 to the given date.
    • Initialize totalDays to 0.
    • Add the days for the full years using (year - 1) * 365.
    • Add an extra day for each leap year passed.
    • Add the days for the months passed in the current year.
    • Add an extra day if the current year is a leap year and the date is past February.
    • Add the days for the current month.
  4. Parse the input dates to extract the year, month, and day.
  5. Calculate the total days for each date using totalDaysToDate.
  6. Return the absolute difference between the total days for each date.
UML Thumbnail

Using Built-in Date Libraries

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...