#include<stdio.h>//计算两个日期间的时间间隔精确到小时,待我突破大数的后,秒不是梦
int a(int y,int m,int d)
{
int ymd[]={31,28,31,30,31,30,31,31,30,31,30,31};
int N=0;
if(m==1)
return N=d;
else
{
for(m=m-1;m>=1;m--)
N+=ymd[m-1];
if(y/4!=0||y/400!=0)
return N+d;
else
return N+1+d;
}
}
void main()
{
int day1,month1,year1,total1,total2,total,left,day2,month2,year2,i,m,n;
printf("请依次输入前面日期的相关数据 年,月,日\n");
scanf("%d%d%d",&year1,&month1,&day1);
total1=a(year1,month1,day1);
printf("请依次输入后面日期的相关数据 年,月,日\n");
scanf("%d%d%d",&year2,&month2,&day2);
total2=a(year2,month2,day2);
if(year1==year2)
total=total2-total1;
else if((year1+1)==year2)
{
if(year1/4!=0||year1/400!=0)
left=365-total1;
else
left=366-total1;
total=left+total2;
}
else
{
if(year1/4!=0||year1/400!=0)
left=365-total1;
else
left=366-total1;
for(;year1<year2;year1++)
if(year1/4!=0||year1/400!=0)
m++;
else
n++;
total=left+total1+365*m+366*n;
}
printf("两个日期间相差天数为:%d\n相差小时数为:%d",total,total*24);
}
C语言计算两个日期间的时间间隔(精确到小时待我突破大数的后,秒不是梦 )
最新推荐文章于 2023-12-20 14:48:11 发布