#include <iostream>
using namespace Std;
static const unsigned char monthtable[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
Class Date
{
Public
Date (int, int month, int day);
Date operator-(const date& date);
Get time difference
int Getdatediff (const date &D1, const date &D2);
void Dispdiff (void);
Public
int year;
int month;
int day;
Time difference
int date_difference;
};
Get the time difference for the same year
int getsameyeardays (const date &D1, const date &D2);
Get the time difference for the same month
int getsamemonthdays (const date &D1, const date &D2);
Get time difference
int getyeardays (const date &D1, const date &D2);
Is the year of the run
BOOL Isleapyear (int year);
Compare Time Size
BOOL Cmpdate (const date &D1, const date &D2);
Date::D ate (int year = $, int month = 1, int day = 1)
{
This->year = year;
This->month = month;
This->day = day;
date_difference = 0;
}
int Date::getdatediff (const date &D1, const date &D2)
{
if (cmpdate (D1, D2))
Return Getyeardays (D1, D2);
Else
Return Getyeardays (D2, D1);
}
Date Date::operator-(const date & date)
{
Date D;
D.date_difference = Getdatediff (*this, date);
return D;
}
void Date::d ispdiff ()
{
cout<< "days =" << date_difference<<endl;
}
Compare Time Size
BOOL Cmpdate (const date &D1, const date &D2)
{
int flage = false;
if (D1.year > D2.year)
{
Flage = true;
}
else if (d1.year = = d2.year)
{
if (D1.month > D2.month)
{
Flage = true;
}
else if (D1.month = = D2.month)
{
if (D1.day >= d2.day)
{
Flage = true;
}
Else
{
Flage = false;
}
}
Else
{
Flage = false;
}
}
Else
{
Flage = false;
}
return flage;
}
BOOL Isleapyear (int year)
{
Return (year% 4 = = 0 && year%100! = 0) | | Year% 400 = = 0? True:false;
}
Get the time difference for the same month
int getsamemonthdays (const date &D1, const date &D2)
{
return d1.day-d2.day;
}
Get the time difference for the same year
int getsameyeardays (const date &D1, const date &D2)
{
int days = 0;
Same month
if (D1.month = = D2.month)
{
Get the time difference for the same month
days = getsamemonthdays (D1, D2);
}
Different months
Else
{
Get the time difference for the first month
Days = monthtable[d2.month-1]-d2.day;
February and is run year days +1
if (D2.month = = 2 && isleapyear (d2.year))
{
days++;
}
Get the time difference for the middle month
for (int i = d2.month + 1; i < d1.month; i++)
{
if (i = = 2 && isleapyear (d1.year))
{
Days + = 29;
}
Else
{
Days + = Monthtable[i-1];
}
}
Get the time difference for the last January
Days + = D1.day;
}
return days;
}
int getyeardays (const date &D1, const date &D2)
{
int days = 0;
Same year
if (d1.year = = d2.year)
{
Get the time difference for the same year
Days = Getsameyeardays (D1,D2);
}
Different years
Else
{
Get the time difference for the first year
Days = Getsameyeardays (Date (d2.year,12,31), D2);
Get the time difference between intermediate years
for (int i = d2.year + 1; i < d1.year; i++)
{
Days + = 365;
if (Isleapyear (i))
{
days++;
}
}
Get the time difference for the last year
Days + = Getsameyeardays (d1, Date (d1.year,1,1)) + 1;
}
return days;
}
int main ()
{
Date D1 (2000,1,1), D2 (2010,1,1);
(D1-D2). Dispdiff ();
return 0;
}
C + + to find the number of days between two dates