VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-number-of-days-between-two-given-dates/

⇱ Days between two given dates - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Days between two given dates

Last Updated : 7 Jun, 2026

Given two dates, find the total number of days between them.

Examples:

Input:
d1 = 10, m1 = 4, y1 = 2013
d2 = 14, m2 = 4, y2 = 2013
Output: 4
Explanation: By counting manually, we find out there are 4 days between the two dates.

Input:
d1 = 10, m1 = 4, y1 = 2001
d2 = 10, m2 = 5, y2 = 2001
Output: 30
Explanation: By counting manually, we find out there are 30 days between the two dates.

[Naive Approach] Using Simulation - O(n) Time O(1) Space

The idea is to simulate the calendar one day at a time starting from the first date and keep moving forward until we reach the second date. Each step represents one day, and we count how many steps are needed.


Output
30

Time Complexity: O(n)
Auxiliary Space: O(1)

[Expected Approach] Absolute Day Count - O(1) Time O(1) Space

The idea is to count all years divisible by 4, then remove the years which are divisible by 100 (since they are not leap years), and finally add back the years which are divisible by 400 (since they are still leap years).


Output
30

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags: