计算日期差多少天

计算日期差多少天

#include
#define yes 1
#define no 0

typedef struct date
{
  int year;
  int month;
  int day;
}DATE;
long timedef(DATE d1,DATE d2);
int leap(int year);
int main()
{
  DATE date1,date2;
  date1.year=2006;
  date1.month=12;
  date1.day=28;
  date2.year=2008;
  date2.month=8;
  date2.day=8;

  printf("%ld",timedef(date1,date2));

}
int leap(int year)
{
  if(year%400==0 ||(year%4==0&&year%100!=0))
   return yes;
  else
   return no;
}
long timedef(DATE d1,DATE d2)
{
  int i;
  long day=0,dayt1=0,dayt2=0;
  int d[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
  for(i=d1.year;i     if(leap(i)==no) day+=365;
  else day+=366;

  for(i=1;i   dayt1+=d[leap(d1.year)][i];
  dayt1+=d1.day;

  for(i=1;i   dayt2+=d[leap(d2.year)][i];
  dayt2+=d2.day;

  return day+dayt2-dayt1;

 
}

Leave a Reply

Your email address will not be published. Required fields are marked *