Find number of days between two given dates
Last Updated :
20 Feb, 2023
Given two dates, find total number of days between them. The count of days must be calculated in O(1) time and O(1) auxiliary space.
Examples:
Input: dt1 = {10, 2, 2014}
dt2 = {10, 3, 2015}
Output: 393
dt1 represents "10-Feb-2014" and dt2 represents "10-Mar-2015"
The difference is 365 + 28
Input: dt1 = {10, 2, 2000}
dt2 = {10, 3, 2000}
Output: 29
Note that 2000 is a leap year
Input: dt1 = {10, 2, 2000}
dt2 = {10, 2, 2000}
Output: 0
Both dates are same
Input: dt1 = {1, 2, 2000};
dt2 = {1, 2, 2004};
Output: 1461
Number of days is 365*4 + 1
One Naive Solution is to start from dt1 and keep counting days till dt2 is reached. This solution requires more than O(1) time.
A Better and Simple solution is to count total number of days before dt1 from i.e., total days from 00/00/0000 to dt1, then count total number of days before dt2. Finally return the difference between two counts.
Let the given two dates be "1-Feb-2000" and "1-Feb-2004"
dt1 = {1, 2, 2000};
dt2 = {1, 2, 2004};
Count number of days before dt1. Let this count be n1.
Every leap year adds one extra day (29 Feb) to total days.
n1 = 2000*365 + 31 + 1 + Number of leap years
Count of leap years for a date 'd/m/y' can be calculated
using the following formula:
Number leap years
= floor(y/4) - floor(y/100) + floor(y/400) if m > 2
= floor((y-1)/4) - floor((y-1)/100) + floor((y-1)/400) if m <= 2
All above divisions must be done using integer arithmetic
So that the remainder is ignored.
For 01/01/2000, leap year count is 1999/4 - 1999/100
+ 1999/400 which is 499 - 19 + 4 = 484
Therefore n1 is 2000*365 + 31 + 1 + 484
Similarly, the count number of days before dt2.
Let this the count be n2.Finally, return n2-n1
Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
struct Date {
int d, m, y;
};
const int monthDays[12]
= { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int countLeapYears(Date d)
{
int years = d.y;
if (d.m <= 2)
years--;
return years / 4
- years / 100
+ years / 400;
}
int getDifference(Date dt1, Date dt2)
{
long int n1 = dt1.y * 365 + dt1.d;
for ( int i = 0; i < dt1.m - 1; i++)
n1 += monthDays[i];
n1 += countLeapYears(dt1);
long int n2 = dt2.y * 365 + dt2.d;
for ( int i = 0; i < dt2.m - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(dt2);
return (n2 - n1);
}
int main()
{
Date dt1 = { 1, 2, 2000 };
Date dt2 = { 1, 2, 2004 };
cout << "Difference between two dates is "
<< getDifference(dt1, dt2);
return 0;
}
|
Java
class GFG
{
static class Date
{
int d, m, y;
public Date( int d, int m, int y)
{
this .d = d;
this .m = m;
this .y = y;
}
};
static int monthDays[] = { 31 , 28 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 };
static int countLeapYears(Date d)
{
int years = d.y;
if (d.m <= 2 )
{
years--;
}
return years / 4 - years / 100 + years / 400 ;
}
static int getDifference(Date dt1, Date dt2)
{
int n1 = dt1.y * 365 + dt1.d;
for ( int i = 0 ; i < dt1.m - 1 ; i++)
{
n1 += monthDays[i];
}
n1 += countLeapYears(dt1);
int n2 = dt2.y * 365 + dt2.d;
for ( int i = 0 ; i < dt2.m - 1 ; i++)
{
n2 += monthDays[i];
}
n2 += countLeapYears(dt2);
return (n2 - n1);
}
public static void main(String[] args)
{
Date dt1 = new Date( 1 , 2 , 2000 );
Date dt2 = new Date( 1 , 2 , 2004 );
System.out.println( "Difference between two dates is " +
getDifference(dt1, dt2));
}
}
|
Python3
class Date:
def __init__( self , d, m, y):
self .d = d
self .m = m
self .y = y
monthDays = [ 31 , 28 , 31 , 30 , 31 , 30 ,
31 , 31 , 30 , 31 , 30 , 31 ]
def countLeapYears(d):
years = d.y
if (d.m < = 2 ):
years - = 1
ans = int (years / 4 )
ans - = int (years / 100 )
ans + = int (years / 400 )
return ans
def getDifference(dt1, dt2):
n1 = dt1.y * 365 + dt1.d
for i in range ( 0 , dt1.m - 1 ):
n1 + = monthDays[i]
n1 + = countLeapYears(dt1)
n2 = dt2.y * 365 + dt2.d
for i in range ( 0 , dt2.m - 1 ):
n2 + = monthDays[i]
n2 + = countLeapYears(dt2)
return (n2 - n1)
dt1 = Date( 1 , 9 , 2014 )
dt2 = Date( 3 , 9 , 2020 )
print ( "Difference between two dates is" ,
getDifference(dt1, dt2))
|
C#
using System;
class GFG {
public class Date {
public int d, m, y;
public Date( int d, int m, int y)
{
this .d = d;
this .m = m;
this .y = y;
}
};
static int [] monthDays = { 31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31 };
static int countLeapYears(Date d)
{
int years = d.y;
if (d.m <= 2) {
years--;
}
return years / 4
- years / 100
+ years / 400;
}
static int getDifference(Date dt1, Date dt2)
{
int n1 = dt1.y * 365 + dt1.d;
for ( int i = 0; i < dt1.m - 1; i++)
{
n1 += monthDays[i];
}
n1 += countLeapYears(dt1);
int n2 = dt2.y * 365 + dt2.d;
for ( int i = 0; i < dt2.m - 1; i++)
{
n2 += monthDays[i];
}
n2 += countLeapYears(dt2);
return (n2 - n1);
}
public static void Main(String[] args)
{
Date dt1 = new Date(1, 2, 2000);
Date dt2 = new Date(1, 2, 2004);
Console.WriteLine( "Difference between two dates is "
+ getDifference(dt1, dt2));
}
}
|
Javascript
<script>
class Date
{
constructor(d,m,y)
{
this .d = d;
this .m = m;
this .y = y;
}
}
let monthDays=[31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31];
function countLeapYears(d)
{
let years = d.y;
if (d.m <= 2)
{
years--;
}
return Math.floor(years / 4) - Math.floor(years / 100) +
Math.floor(years / 400);
}
function getDifference(dt1,dt2)
{
let n1 = dt1.y * 365 + dt1.d;
for (let i = 0; i < dt1.m - 1; i++)
{
n1 += monthDays[i];
}
n1 += countLeapYears(dt1);
let n2 = dt2.y * 365 + dt2.d;
for (let i = 0; i < dt2.m - 1; i++)
{
n2 += monthDays[i];
}
n2 += countLeapYears(dt2);
return (n2 - n1);
}
let dt1 = new Date(1, 2, 2000);
let dt2 = new Date(1, 2, 2004);
document.write( "Difference between two dates is " +
getDifference(dt1, dt2));
</script>
|
Output
Difference between two dates is 1461
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The above program follows the Gregorian Calendar from the beginning of the time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...