LeetCode Problem

How to Solve Number of Days Between Two Dates

To solve this problem, convert each date string into a total day count from a fixed reference point, such as 1900-01-01. Subtract these totals to get the absolute number of days between the two dates. This approach ensures correct handling of leap years and varying month lengths, relying on precise math and string operations for parsing.

GhostInterview Help

Need help with Number of Days Between Two Dates without spending extra time grinding it?

GhostInterview can read Number of Days Between Two Dates from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1360Math plus StringReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Math plus String
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, convert each date string into a total day count from a fixed reference point, such as 1900-01-01. Subtract these totals to get the absolute number of days between the two dates. This approach ensures correct handling of leap years and varying month lengths, relying on precise math and string operations for parsing.

Problem Statement

Given two dates as strings in the format YYYY-MM-DD, write a program to determine the number of days between them. The solution must correctly handle leap years and varying month lengths, and return a non-negative integer representing the difference in days.

For example, given date1 = "2019-06-29" and date2 = "2019-06-30", the output should be 1. Another example is date1 = "2020-01-15" and date2 = "2019-12-31", which should return 15. Constraints: all dates are valid between 1971 and 2100.

Examples

Example 1

Input: date1 = "2019-06-29", date2 = "2019-06-30"

Output: 1

Example details omitted.

Example 2

Input: date1 = "2020-01-15", date2 = "2019-12-31"

Output: 15

Example details omitted.

Constraints

  • The given dates are valid dates between the years 1971 and 2100.

Solution Approach

Parse Dates into Year, Month, Day

Split each input string by the '-' character to extract integers representing year, month, and day. This string-to-integer parsing is critical to feed the arithmetic calculations accurately.

Compute Days from a Fixed Reference

Implement a helper function f(date) that counts the total number of days from a base date like 1900-01-01 to the given date. Include leap year logic and month day accumulation. Subtract f(date1) from f(date2) and return the absolute value.

Handle Leap Years and Month Lengths

Use standard rules for leap years: divisible by 4 but not 100 unless divisible by 400. Maintain an array of month lengths for non-leap and leap years. This prevents off-by-one errors in the final day count.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(1) because date calculations involve fixed-size arithmetic regardless of input. Space complexity is O(1) as only constant variables and arrays for month lengths are used.

What Interviewers Usually Probe

  • Expect clear handling of string parsing and date conversion logic.
  • Look for awareness of leap years and month-specific day counts.
  • Check for simplicity in computing total days without iterative counting from one date to another.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring leap years and producing off-by-one errors for February dates.
  • Swapping date1 and date2 incorrectly or not taking absolute difference.
  • Assuming all months have 30 or 31 days instead of using correct month lengths.

Follow-up variants

  • Calculate business days only between two dates, skipping weekends.
  • Include time in hours and minutes to compute the difference precisely.
  • Determine the day of the week for each date in addition to the difference.

How GhostInterview Helps

  • Provides a step-by-step method to convert date strings into day counts efficiently.
  • Highlights common mistakes in leap year handling and month length calculations.
  • Generates examples and edge cases that verify the arithmetic logic before coding.

Topic Pages

FAQ

What is the main approach to solve Number of Days Between Two Dates?

Convert each date to total days from a fixed reference using arithmetic and string parsing, then take the absolute difference.

How do leap years affect the calculation?

Leap years add an extra day in February, so the helper function must account for years divisible by 4, excluding 100 unless divisible by 400.

Can I assume months have fixed lengths?

No, each month has a specific number of days, and February varies by leap year; using an array of month lengths avoids miscalculations.

Do I need to worry about negative results?

No, always return the absolute difference between the two day counts regardless of which date comes first.

Is this problem best categorized under Math plus String?

Yes, it requires parsing strings into numerical components and applying arithmetic to compute the day difference accurately.

GhostInterview Solver

Need direct help with Number of Days Between Two Dates instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Number of Days Between Two Dates from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.