❶ 如何用c语言计算日期间离天数
我学C#、Java的,对C不是很了解,仅仅提供个思路:
请问一下C有没有日期函数?有吧应该?
如果有的话,是不是能返回自1970年1月1日起到你输入的日期的毫秒数呢?
能返回的话,你输入的两个时间都换成毫秒数,一减,得到的不就是两个
日期相差的毫秒数?
然后把毫秒数换算成天数不就成了?
❷ C语言计算两日期间的天数
//用 <time.h> 中的 time_t 和 struct tm 结构体, difftime() 函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
time_t maketime_t(char* date1)
{
struct tm tm0;
char str0[4];
//date1
memset( &tm0, 0, sizeof(tm0));
memset( str0, 0, 4);
strncpy(str0, date1, 4);
tm0.tm_year = atoi(str0) - 1900; //1900
memset( str0, 0, 4);
strncpy(str0, &date1[4], 2);
tm0.tm_mon = atoi(str0) - 1; //0-11
memset( str0, 0, 4);
strncpy(str0, &date1[6], 2);
tm0.tm_mday = atoi(str0); //1-31
return mktime(&tm0);
}
int day_of_day( char *date1, char *date2)
{
struct tm tm0;
char str0[4];
double diff_seconds;
time_t time_t1, time_t2;
time_t1 = maketime_t(date1);
time_t2 = maketime_t(date2);
diff_seconds = difftime(time_t1, time_t2);
return diff_seconds>0?(diff_seconds/3600/24):((-1)*diff_seconds/3600/24);
}
int main()
{
int diff_days;
diff_days = day_of_day("20121214", "20121211");
printf("diff_days=%d\n", diff_days);
return 0;
}
❸ 用c语言编程计算两个日期之间的天数
直接算两个日期距离公元元年1月1日的天数:
若干个完整的年,若干个完整的月,若干天,加起来之后两个天数相减就行了.
日期的合法性,闰年之类的就不罗嗦了.
❹ C语言中 求两个日期间的天数
以前做的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((year1>year2)||((year1==year2)&&(month1>month2))||((year1==year2)&&(month1==month2)&&(day1>day2)))
{
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(*day>mon[*month-1]||*month>13||*year<1900||*year>3000);
mon[1]=28;
}
long com_year(int y1,int y2) /*计算两个年份之间的天数*/
{
int i;
long sum=0;
for(i=y1+1;i<y2;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;i<12;i++)
{
sum+=mon[i];
}
mon[1]=28;
if(spec_year(y2))
{
mon[1]=29;
}
for(i=0;i<m2-1;i++)
{
sum+=mon[i];
}
mon[1]=28;
}
else
{
for(i=m1-1;i<12;i++)
{
sum+=mon[i];
}
mon[1]=28;
if(spec_year(y2))
{
mon[1]=29;
}
for(i=0;i<m2-1;i++)
{
sum+=mon[i];
}
mon[1]=28;
}
}
else
{
if(spec_year(y1))
{
mon[1]=29;
}
for(i=m1;i<m2-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;
}
❺ 用c语言编写计算两个日期之间的天数
#include <time.h>
#include <stdio.h>
void main()
{
tm t1={0},t2={0};
t1.tm_year = 1999-1900;
t1.tm_mon = 2-1;
t1.tm_mday = 3;
t2.tm_year = 2015-1900;
t2.tm_mon = 5-1;
t2.tm_mday = 4;
time_t a1 = mktime(&t1);
time_t a2 = mktime(&t2);
printf("相差%d天", (a2-a1)/86400);
}
ps:看不懂了再追问
❻ 急求!C语言计算天数
#include<stdio.h>
int main()
{
int yyyy,mm,dd,a,b;
scanf("%d/%d/%d",&yyyy,&mm,&dd);
a=31*(mm>1)+28*(mm>2)+31*(mm>3)+30*(mm>4)+31*(mm>5)+30*(mm>6)+31*(mm>7)+31*(mm>8)+30*(mm>9)+31*(mm>10)+30*(mm>11)+dd;
b=((yyyy%4==0)*(yyyy%100!=0)+(yyyy%400==0))*(mm>2);
printf("%d\n",a+b);
return 0;
}
❼ C语言求两个日期相差的天数
计算两个年月日之间的天数,思路是分别算出日期的总天数然后相减。
要考虑闰年的情况,判断闰年的口诀:4年一闰,100年不闰,400年再闰。
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
网上找了一个(偷懒= =!),修改下如下:
#include <stdio.h>
int sum(int y,int m,int d)
{
unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,s=0;
for(i=1;i<y;i++)
if(i%4==0 && i%100!=0 || i%400==0)
s+=366;//闰年
else
s+=365;//平年
if(y%4==0 && y%100!=0 || y%400==0)
x[2]=29;
for(i=1;i<m;i++)
s+=x[i];//整月的天数
s+=d;//日的天数
return s;//返回总天数,相对公元1年
}
void main()
{
unsigned char y1,m1,d1,y2,m2,d2;
int s1,s2;
printf("输入第一个年 月 日:");
scanf("%d %d %d",&y1,&m1,&d1);
printf("输入第二个年 月 日:");
scanf("%d %d %d",&y2,&m2,&d2);
s1=sum(y1,m1,d1);
s2=sum(y2,m2,d2);
if (s1 > s2)
printf("相差天数:%ld\n",s1-s2);
else
printf("相差天数:%ld\n",s2-s1);
}
以上代码VC6编译测试通过。
虽然这个思路显得有些笨,但是其它算法,代码太长太复杂,要考虑多种情况,不如直接算两个日期距离公元元年1月1日的天数,然后相减
❽ 给定年月日 怎样用C语言编程计算2个日期之间的时间天数
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;i<M;i++) JD+=MonthDay[i];
if (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;
return JD;
}
整年的天数,涉及闰年的判断:
某年是否闰年,用 (Y%4==0)&&(Y%100!=0)||(Y%400==0) 判断。闰年366天,平年365天。 有了这些,写程序不难。
未考虑公元前的年月日计算。