C语言生日到当前日期时间差计算
发布网友
发布时间:2022-04-22 14:13
我来回答
共3个回答
热心网友
时间:2023-07-05 09:35
给个模板,造福人类
//-------------------从X年X月X日开始到X年X月X日之间多少天了
#include<stdio.h>
//给定一个年月日,计算这一年已经过的天数
int total_day(int year,int month,int day)
{
int sum = 0;
switch(month)
{
case 1:sum = day;
break;
case 2:sum = 31 + day;
break;
case 3:sum = 59 + day;
break;
case 4:sum = 90 + day;
break;
case 5:sum = 120 + day;
break;
case 6:sum = 151 + day;
break;
case 7:sum = 181 + day;
break;
case 8:sum = 212 + day;
break;
case 9:sum = 243 + day;
break;
case 10:sum = 273 + day;
break;
case 11:sum = 304 + day;
break;
case 12:sum = 334 + day;
break;
default :
printf("输入的月份有错误\n");
break;
}
if(month >2)
{
if(((year % 4 == 0)&&(year % 100 !=0))||(year % 400) == 0){
sum = sum + 1;
}
}
return sum;
}
//年与年相差的天数
int total_year_day(int year1,int year2)
{
int sum_year_day = 0;
int i = 0;
sum_year_day = (year2 - year1) * 365;
for(i=year1 ; i<year2 ; i++)
{
if(((i % 4 == 0)&&(i % 100 !=0))||(i % 400) == 0){
sum_year_day = sum_year_day + 1;
}
}
return sum_year_day;
}
int main()
{
int year1 = 2013, month1 = 1,day1 = 1;
int year2 = 2013, month2 = 1,day2 = 1;
int sum = 0;
printf("~~~~~~计算两天之间相差的天数~~~~~~\n");
printf("请输入起始的年月日(格式为:XXXX:XX:XX)");
scanf("%d:%d:%d",&year1,&month1,&day1);
printf("请输入结束的年月日(格式为:XXXX:XX:XX)");
scanf("%d:%d:%d",&year2,&month2,&day2);
sum = total_year_day(year1,year2) - total_day(year1,month1,day1) + total_day(year2,month2,day2);
printf("它们之间相差的天数为:%d\n",sum);
return 0;
}
热心网友
时间:2023-07-05 09:35
差天数 = ((年-年)*365+中间闰年数) + ((月-月)*30+中间大月数-2*中间有2月flag + 中间有闰2月flag) + (日-日)
热心网友
时间:2023-07-05 09:36
运行过了么有任何问题,有什么不明白的可以交流下!!
#include <stdio.h>
#include<time.h>
//计算给定的日期是本年的第几天
int count(int year,int month,int day)
{int i,sum=0,flag=0;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<month;i++)
sum=a[i]+sum;
if(year%400==0||year%100!=0&&year%4==0)
flag=1;
if(flag==1&&month>2) sum++;
sum=sum+day;
return sum;
}
int main()
{
int year,month,day;
time_t t;
struct tm *local;
int sum=0,i;
t=time(NULL);
local=localtime(&t);
printf("今天的日期:%d年%d月%d日\n",local->tm_year+1900,local->tm_mon+1,local->tm_mday);
printf("输入生日:");
scanf("%d%d%d",&year,&month,&day);
for(i=year+1;i<local->tm_year+1900;i++)
{
if(i%400==0||i%100!=0&&i%4==0)
sum+=366;
else sum+=365;
}
if((year%400==0||year%100!=0&&year%4==0))
sum+=366-count(year,month,day);
else sum+=365-count(year,month,day);
sum+=count(local->tm_year+1900,local->tm_mon+1,local->tm_mday);
printf("%d年%d月%d日距今天有%d天\n",year,month,day,sum);
return 0;
}
c编程高手团队正在招新,有意者速速行动,一起学习,一起努力!!