重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
int GetMonthDays(int year,int month)
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站制作、房山网络推广、小程序开发、房山网络营销、房山企业策划、房山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供房山建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
case 2:
if((year%4==0 year%100!=0 ) || (year%400==0))
{
return 29;
}
else
{
return 28;
}
break;
default:
printf("month error!");
return 0;
break;
}
}
#include stdio.h
#include stdlib.h
#include string.h
#include math.h
#include time.h
int get_days(const char* from, const char* to);
time_t convert(int year,int month,int day);
int main()
{
const char* from="2013-3-15";
const char* to="2015-8-14";
int days=get_days(from,to);
printf("From:%s\nTo:%s\n",from,to);
printf("%d\n",days);
return 0;
}
time_t convert(int year,int month,int day)
{
struct tm info={0};
info.tm_year=year-1900;
info.tm_mon=month-1;
info.tm_mday=day;
return mktime(info);
}
int get_days(const char* from, const char* to)
{
int year,month,day,fromSecond,toSecond;
sscanf(from,"%d-%d-%d",year,month,day);
fromSecond=(int)convert(year,month,day);
sscanf(to,"%d-%d-%d",year,month,day);
toSecond=(int)convert(year,month,day);
return (toSecond-fromSecond)/24/3600;
}
From:2013-3-15
To:2015-8-14
882
Press any key to continue
这才算是用了库函数了···
1970 年以后的时间,可以用 time.h 里的函数计算。时间精度为秒。按题目要求,输出时间单位用天。程序如下:
#include stdio.h
#include time.h
time_t YMD_hhmmss_2_s70(int Y, int M, int D, int hh, int mm, int ss){
struct tm *target_tm;
time_t tt;
time (tt);
target_tm=localtime(tt);
target_tm-tm_year = Y - 1900;
target_tm-tm_mon= M - 1;
target_tm-tm_mday = D;
target_tm-tm_hour = hh; // hour
target_tm-tm_min = mm;
target_tm-tm_sec = ss;
tt = mktime(target_tm); // from tm to time_t (s)
return tt;
}
int main()
{
int y1,m1,d1,y2,m2,d2;
time_t t1,t2;
int dt;
printf("input y1 m1 d1: ");
scanf("%d %d %d",y1,m1,d1);
printf("\ninput y2 m2 d2: ");
scanf("%d %d %d",y2,m2,d2);
t1 = YMD_hhmmss_2_s70(y1,m1,d1,0,0,0);
t2 = YMD_hhmmss_2_s70(y2,m2,d2,0,0,0);
dt = (t2-t1)/(24*3600);
printf("\ndt=%d\n",dt);
return 0;
}
这里未包含日期的合法性判断。
1970 年以前 要另写程序。某年的日子是当年的第几天可用下面函数得出:
int YMD_2_JD(int Y, int M, int D){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=D;
for (i=0;iM;i++) JD+=MonthDay[i];
if (((Y%4==0)(Y%100!=0)||(Y%400==0)) (M2)) JD++;
return JD;
}
整年的天数,涉及闰年的判断:
某年是否闰年,用 (Y%4==0)(Y%100!=0)||(Y%400==0) 判断。闰年366天,平年365天。 有了这些,写程序不难。
未考虑公元前的年月日计算。
以前做的C语言实习作业......
要把年、月、日分开来求...
#include stdio.h
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31}; /*储存12个月的天数*/
void main()
{
int spec_year(int);
void scan(int *year,int *month,int *day);
long com_year(int y1,int y2);
int com_month(int y1,int m1,int y2,int m2);
int com_day(int y1,int m1,int d1,int y2,int m2,int d2);
int year1,month1,day1;
int year2,month2,day2;
long sum=0;
int k=0;
/*输入两个日期,做合法性检查*/
printf("Enter the first date,like 1988 11 30:");
scan(year1,month1,day1);
printf("Enter the second date,like 1988 11 30:");
scan(year2,month2,day2);
/*当输入的第一个日期比第二个日期大时,将二者对换*/
if((year1year2)||((year1==year2)(month1month2))||((year1==year2)(month1==month2)(day1day2)))
{
int temp;
temp=year1;year1=year2;year2=temp;
temp=month1;month1=month2;month2=temp;
temp=day1;day1=day2;day2=temp;
}
/*计算两个日期之间的天数*/
sum+=com_year(year1,year2); /*计算两个年份之间的天数*/
sum+=com_month(year1,month1,year2,month2); /*计算两个月份之间的天数*/
sum+=com_day(year1,month1,day1,year2,month2,day2); /*计算两天之间的天数*/
/*输出*/
printf("There are %ld days between them.\n",sum);
}
void scan(int *year,int *month,int *day) /*输入日期函数,合法性判断*/
{
int k=0;
do
{
if(k!=0)
printf("\nWrong date!\n");
k++;
/*printf("Please write down the day,like 1988 11 30:");*/
scanf("%d%d%d",year,month,day);
if(spec_year(*year))
mon[1]=29;
}while(*daymon[*month-1]||*month13||*year1900||*year3000);
mon[1]=28;
}
long com_year(int y1,int y2) /*计算两个年份之间的天数*/
{
int i;
long sum=0;
for(i=y1+1;iy2;i++)
{
if(spec_year(i))
sum+=366;
else
sum+=365;
}
return sum;
}
int com_month(int y1,int m1,int y2,int m2) /*计算两个月份之间的天数*/
{
int i;
int sum=0;
if(y1!=y2)
{
if(spec_year(y1))
{
mon[1]=29;
}
if(m1!=m2)
{
for(i=m1;i12;i++)
{
sum+=mon[i];
}
mon[1]=28;
if(spec_year(y2))
{
mon[1]=29;
}
for(i=0;im2-1;i++)
{
sum+=mon[i];
}
mon[1]=28;
}
else
{
for(i=m1-1;i12;i++)
{
sum+=mon[i];
}
mon[1]=28;
if(spec_year(y2))
{
mon[1]=29;
}
for(i=0;im2-1;i++)
{
sum+=mon[i];
}
mon[1]=28;
}
}
else
{
if(spec_year(y1))
{
mon[1]=29;
}
for(i=m1;im2-1;i++)
{
sum+=mon[i];
}
mon[1]=28;
}
return sum;
}
int com_day(int y1,int m1,int d1,int y2,int m2,int d2) /*计算两天之间的天数*/
{
int i;
int sum=0;
if(m1!=m2)
{
if(spec_year(y1))
{
mon[1]=29;
}
sum+=(mon[m1-1]-d1);
sum+=d2;
mon[1]=28;
}
else
{
sum+=d2-d1;
}
return sum;
}
int spec_year(int year) /*判断是否为闰年*/
{
if((year%400==0)||((year%4==0)(year%100!=0)))
return 1;
else
return 0;
}