当前位置: 编程技术>c/c++/嵌入式
C语言小程序 如何判断两个日期之差
来源: 互联网 发布时间:2014-10-18
本文导语: 1.普通的写法 代码如下:#include int leapyear(int year){ if((year%4==0 && year%100!=0) || year%400==0) return 1; else return 0;}int days(int *day1, int *day2){ int i=0; int *tmp; int diff = 0; const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(day1[0] == day2[0])...
1.普通的写法
#include
int leapyear(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return 1;
else
return 0;
}
int days(int *day1, int *day2)
{
int i=0;
int *tmp;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(day1[0] == day2[0])
{
if(day1[1] == day2[1])
{
diff = day1[2] - day2[2];
diff = (diff < 0)?(-diff):diff;
}
else
{
if(day1[1] < day2[1]) //day1=1991-5-8 day2=1991-6-2
{
tmp = day1; //day1=1991-6-2 day2=1991-5-8
day1 = day2;
day2 = tmp;
}
for(i=day2[1]+1; i d2->month) //月数相等
{
tmp = d1;
d1 = d2;
d2 = d1;
}
else if(d1->month == d2->month) //日期相等
{
if(d1->day > d2->day)
{
tmp = d1;
d1 = d2;
d2 = d1;
}
}
}
else if(d1->year > d2->year)
{
tmp = d1;
d1 = d2;
d2 = tmp;
}
return 0;
}
int diff(DATE *date1, DATE *date2)
{
int i;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date1->year == date2->year)
{
if(date1->month == date2->month)
{
diff = date2->day - date1->day;
}
else
{
for(i=date1->month+1; imonth; i++)
{
diff += month[i];
}
diff += month[date1->month] - date1->day + date2->day;
if(leapyear(date1->year))
if(date1->month month >2)
diff++;
}
}
else
{
for(i=date1->year+1; iyear; i++)
{
if(leapyear(i))
diff += 366;
else
diff += 365;
}
for(i=date1->month+1; imonth] - date1->day;
if(date1->month year))
diff++;
for(i=1; imonth; i++) //date2距离年初多少天
{
diff += month[i];
}
diff += date2->day;
if(date1->month > 2)
if(leapyear(date2->year))
diff++;
}
return diff;
}
int main()
{
int days = 0;
DATE day1, day2;
DATE *date1, *date2;
date1 = &day1;
date2 = &day2;
printf("输入日期:");
scanf("%d-%d-%d",&(date1->year), &(date1->month), &(date1->day));
printf("输入另一个日期:");
scanf("%d-%d-%d",&date2->year, &date2->month, &date2->day);
compare(date1, date2);
days = diff(date1, date2);
printf("两个日期之间共有%d天。n",days);
return 0;
}
代码如下:
#include
int leapyear(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return 1;
else
return 0;
}
int days(int *day1, int *day2)
{
int i=0;
int *tmp;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(day1[0] == day2[0])
{
if(day1[1] == day2[1])
{
diff = day1[2] - day2[2];
diff = (diff < 0)?(-diff):diff;
}
else
{
if(day1[1] < day2[1]) //day1=1991-5-8 day2=1991-6-2
{
tmp = day1; //day1=1991-6-2 day2=1991-5-8
day1 = day2;
day2 = tmp;
}
for(i=day2[1]+1; i d2->month) //月数相等
{
tmp = d1;
d1 = d2;
d2 = d1;
}
else if(d1->month == d2->month) //日期相等
{
if(d1->day > d2->day)
{
tmp = d1;
d1 = d2;
d2 = d1;
}
}
}
else if(d1->year > d2->year)
{
tmp = d1;
d1 = d2;
d2 = tmp;
}
return 0;
}
int diff(DATE *date1, DATE *date2)
{
int i;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date1->year == date2->year)
{
if(date1->month == date2->month)
{
diff = date2->day - date1->day;
}
else
{
for(i=date1->month+1; imonth; i++)
{
diff += month[i];
}
diff += month[date1->month] - date1->day + date2->day;
if(leapyear(date1->year))
if(date1->month month >2)
diff++;
}
}
else
{
for(i=date1->year+1; iyear; i++)
{
if(leapyear(i))
diff += 366;
else
diff += 365;
}
for(i=date1->month+1; imonth] - date1->day;
if(date1->month year))
diff++;
for(i=1; imonth; i++) //date2距离年初多少天
{
diff += month[i];
}
diff += date2->day;
if(date1->month > 2)
if(leapyear(date2->year))
diff++;
}
return diff;
}
int main()
{
int days = 0;
DATE day1, day2;
DATE *date1, *date2;
date1 = &day1;
date2 = &day2;
printf("输入日期:");
scanf("%d-%d-%d",&(date1->year), &(date1->month), &(date1->day));
printf("输入另一个日期:");
scanf("%d-%d-%d",&date2->year, &date2->month, &date2->day);
compare(date1, date2);
days = diff(date1, date2);
printf("两个日期之间共有%d天。n",days);
return 0;
}