c语言两日期相差天数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言两日期相差天数相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include<math.h>
void main()
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]=31,28,31,30,31,30,31,31,30,31,30,31,b[12]=31,29,31,30,31,30,31,31,30,31,30,31;
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
if(y1%100!=0&&y1%4==0||y1%400==0)
while(m1<m2)
s=b[m1-1]+s;
m1=m1+1;
else
while(m1<m2)
s=a[m1-1]+s;
m1=m1+1;
printf("%d days\n",s+d2-d1);
else
while(y1<y2)
if(m1<=2)
if(y1%100!=0&&y1%4==0||y1%400==0)
s=366+s;
else
s=365+s;
y1=y1+1;
else
y1=y1+1;
if(y1%100!=0&&y1%4==0||y1%400==0)
s=366+s;
else
s=365+s;
if(m1<=m2)
if(y2%100!=0&&y2%4==0||y2%400==0)
for(i=0;m1<m2;i++)
s=b[m1-1]+s;
m1=m1+1;
else
for(i=0;m1<m2;i++)
s=a[m1-1]+s;
m1=m1+1;
printf("%d days\n",s+d2-d1);
else
if(y2%100!=0&&y2%4==0||y2%400==0)
while(m1>m2)
s=b[m1-1]+s;
m1=m1-1;
else
while(m1>m2)
s=a[m1-1]+s;
m1=m1-1;
printf("%d days\n",s+d2-d1);
求高手指点错误所在
大体说下我的思路:
1.判断是否是同一年。
2.不同年,先计算相差的年数,再计算月份,最后计算天数
#include<math.h>
int main()
int s=0,m1,d1,y1,y2,m2,d2,i,a[12]=31,28,31,30,31,30,31,31,30,31,30,31,b[12]=31,29,31,30,31,30,31,31,30,31,30,31;
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1==y2)
if((y1%100!=0&&y1%4==0)||y1%400==0)
while(m1<m2)
s=b[m1-1]+s;
m1=m1+1;
else
while(m1<m2)
s=a[m1-1]+s;
m1=m1+1;
printf("%d days\n",s+d2-d1);
else
while(y1<y2)
if(m1<=2)
if((y1%100!=0&&y1%4==0)||y1%400==0)
s=366+s;
else
s=365+s;
y1=y1+1;
else
y1=y1+1;
if((y1%100!=0&&y1%4==0)||y1%400==0)
s=366+s;
else
s=365+s;
if(m1<=m2)
if((y2%100!=0&&y2%4==0)||y2%400==0)
for(i=0;m1<m2;i++)
s=b[m1-1]+s;
m1=m1+1;
else
for(i=0;m1<m2;i++)
s=a[m1-1]+s;
m1=m1+1;
printf("%d days\n",s+d2-d1);
else
if((y2%100!=0&&y2%4==0)||y2%400==0)
while(m1>m2)
s=b[m1-1]+s;
m1=m1-1;
else
while(m1>m2)
s=a[m1-1]+s;
m1=m1-1;
printf("%d days\n",s+d2-d1);
return 0;
你试试看!追问
还是吧对。。你改哪了?
追答if后面多加了一个括号!
参考技术A 说实话最好的方式是用时间戳的方式。在类unix系统中时间戳是从1970年1月1日00:00到某时间所经历的毫秒数,转化后两者相减再转化为天数就得出结果了。"2014-04-09 22:00:02" => 2312312312321(到现在为止的时间长度)
"2013-12-11 12:33:12" => 231212311111
两者相差的毫秒数 => 转为天数
以上是关于c语言两日期相差天数的主要内容,如果未能解决你的问题,请参考以下文章
c语言怎么求两个日期相差的秒数,日期格式20140325150630和20140324150000