The calculation principle is to first find the difference of the number of days between each date and January 1, January 1, and then perform the difference.
[Cpp] # include <stdio. h>
Struct MyDate
{
Int year;
Int month;
Int day;
};
Int GetAbsDays (MyDate x)
{
Int I;
Int month_day [] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31 };
Int year = x. year-1; // The distance from January 1, January 1.
Int days = year * 365 + year/4-year/100 + year/400; // calculate the number of previous leap years and add the number of days
If (x. year % 4 = 0 & x. year % 100! = 0 | x. year % 400 = 0) month_day [1] ++; // the current year is a leap year, and 1 is added on January 1, February.
For (I = 0; I <x. month-1; I ++)
Days + = month_day [I];
Days + = x. day-1; // today is not counted as the number of days
Return days;
}
Int GetDiffDays (MyDate a, MyDate B)
{
Return GetAbsDays (B)-GetAbsDays ();
}
Int main (int argc, char * argv [])
{
MyDate a = {1842,5, 18 };
MyDate B = {2000,3, 13 };
Int n = GetDiffDays (a, B );
Printf ("% d \ n", n );
}