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.
CopyCode The Code is as follows: # 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 );
}