问题描述:如2010-03-20与2020-03-20,相差3653天。 方法描述:分别计算两个时间点距离0000-00-00时的天数,然后取差值 编译环境:vc++6.0 代码:
#include
#include
#define leapYear(y) ((y%4==0)&&(y%100!=0)||(y%400==0)) //宏定义,判断是否是闰年
/*计算两个日期之间相差多少天*/
struct date
{
int years;
int months;
int days;
};
int days_on_month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //分别对应1到12月的天数,a[0]空着
int days_date(struct date s) //以0000-00-00时为基准,计算过去了多少天
{
int sum=0; //统计天数
for (int i = 0; i
return abs(days_date(s1)-days_date(s2));
}
int main()
{
struct date s1, s2;
printf("输入日期1(yyyy-mm-dd):\n");
scanf("%d-%d-%d", &s1.years, &s1.months, &s1.days);
printf("输入日期2(yyyy-mm-dd):\n");
scanf("%d-%d-%d", &s2.years, &s2.months, &s2.days);
printf("相差%d天\n", diff( s1, s2));
return 0;
}
测试案例: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200320103807183.png)
|