Number of Days Between Two Dates — Easy Problem & Solution

Given two valid dates date1 and date2, each written as YYYY-MM-DD, return the number of days between them — the absolute difference, so the order of the two…

  • Difficulty: Easy
  • Topics: Strings, Math
  • Asked at: Amazon, Microsoft
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given two valid dates date1 and date2, each written as YYYY-MM-DD, return the number of days between them — the absolute difference, so the order of the two dates does not matter.

Use the Gregorian calendar: a year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400.

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

Example 3

Input: date1 = "2099-12-31", date2 = "2100-03-01"
Output: 60
Explanation: 2100 is not a leap year: 31 days of January, 28 of February, then March 1st.

Constraints

  • date1 and date2 are valid dates between the years 1971 and 2100 (inclusive)
  • Both are written as YYYY-MM-DD

How to solve Number of Days Between Two Dates

Map each date to the number of days since a fixed epoch (1971-01-01); the distance is the absolute difference of the two numbers.

Approach

  1. Parse year, month and day from each string.
  2. Count days since the epoch: 365 or 366 for every full year from 1971 to year - 1, the lengths of the earlier months of year (February 29 days in a leap year), plus day.
  3. Return |count(date1) - count(date2)|.

Why it works

Counting from a common origin turns each date into a position on one number line, and the gap between two positions is their difference, independent of which comes first. Summing whole years, then whole months, then days counts each calendar day exactly once.

Complexity

  • Time — O(Y) for the year loop (at most 130 years); O(1) with a closed-form leap count
  • Space — O(1)

Pitfalls

  • 2100 is not a leap year, 2000 is.
  • Forgetting the absolute value when date1 is later than date2.
  • Off-by-one when converting: count both dates the same way and the offsets cancel.

Reference solution

Python

def daysBetweenDates(date1: str, date2: str) -> int:
    def is_leap(y: int) -> bool:
        return (y % 4 == 0 and y % 100 != 0) or y % 400 == 0

    def days_since_epoch(date: str) -> int:
        year, month, day = int(date[:4]), int(date[5:7]), int(date[8:])
        lengths = [31, 29 if is_leap(year) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        total = day
        for y in range(1971, year):
            total += 366 if is_leap(y) else 365
        return total + sum(lengths[:month - 1])

    return abs(days_since_epoch(date1) - days_since_epoch(date2))

JavaScript

var daysBetweenDates = function(date1, date2) {
    var isLeap = function(y) { return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0; };
    var count = function(date) {
        var year = parseInt(date.substring(0, 4), 10);
        var month = parseInt(date.substring(5, 7), 10);
        var total = parseInt(date.substring(8, 10), 10);
        var lengths = [31, isLeap(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        for (var y = 1971; y < year; y++) total += isLeap(y) ? 366 : 365;
        for (var m = 0; m < month - 1; m++) total += lengths[m];
        return total;
    };
    return Math.abs(count(date1) - count(date2));
};

Also on the editorial tab: C, C#, C++, Go, Java, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 424 strings problems · the whole catalogue

Learn the technique: Strings