如何使用c语言计算日期差异 [英] How to calculate the date difference using c languvage

查看:85
本文介绍了如何使用c语言计算日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,



如何计算今天(到)最后一天的日期差异。



i希望这个程序使用C Languvage。



请给我回答。



例如:



今日日期:18.08.2012

最后一天:30.05.2012

___________

答案12.03 .21

dear sir,

How to calculate the date difference in today (to) last day .

i want this program using C Languvage.

please give me answer.

Example:

Today Date:18.08.2012
last day:30.05.2012
___________
Answer 12.03 .21

推荐答案

查看这些链接..这将解决您的问题..

< a href =http://cboard.cprogramming.com/c-programming/97842-getting-difference-between-dates.html> http://cboard.cprogramming.com/c-programming/97842-getting-difference -between-dates.html [ ^ ]

HTTP://cboard.cprogra mming.com/c-programming/108714-review-required-program-date-difference.html [ ^ ]

http://stackoverflow.com/questions/1444428/time-stamp-in-the-c-programming-language [ ^ ]
See these links..it will solve your problem..
http://cboard.cprogramming.com/c-programming/97842-getting-difference-between-dates.html[^]
http://cboard.cprogramming.com/c-programming/108714-review-required-program-date-difference.html[^]
http://stackoverflow.com/questions/1444428/time-stamp-in-the-c-programming-language[^]


包括< stdio.h>





包括< conio.h>

< br $>


int main()

{

int fd,fm,fy,sd,sm,sy,ty ,tday,rday,i,r,c1,c2;

printf(条目类似日期< space> Month< space> Year \ n);

printf (输入你的第一个日期:);

scanf(%d%d%d,& fd,& fm,& fy);

的printf(恩特r你的第二个日期:);

scanf(%d%d%d,& sd,& sm,& sy);

ty = fy-sy;

tday = ty * 365;

tday--;

fy--;

for(i = sy; i< = fy; i ++)

{

r = i%4;

if(r == 0)

{

tday ++;

}

}

c1 = - fm ;

开关(c1)

{

案例1:tday = tday + 31;休息;

案例2:tday = tday + 59; fy ++; r = fy%4; if(r == 0){tday ++;} break;

case 3:tday = tday + 90; fy ++; r = fy%4; if(r == 0){tday ++;} break;

case 4:tday = tday + 120; fy ++; r = fy%4; if(r == 0){ tday ++;} break;

case 5:tday = tday + 151; fy ++; r = fy%4; if(r == 0){tday ++;} break;

情况6:tday = tday + 181; fy ++; r = fy%4; if(r == 0){tday ++;} break;

case 7:tday = tday + 212; fy ++; r = fy%4; if(r == 0){tday ++;} break;

case 8:tday = tday + 243; fy ++; r = fy%4; if(r = = 0){tday ++;} break;

case 9:tday = tday + 273; fy ++; r = fy%4; if(r == 0){tday ++;} break;

案例10:tday = tday + 304; fy ++; r = fy%4; if(r == 0){tday ++;} break;

case 11:tday = tday + 334; fy ++; r = fy%4; if(r == 0){tday ++;} break;

}

tday = tday + fd;

c2 = - sm;

开关(c2)

{

案例1:rday = 31;休息;

案例2:rday = 59; r = sy%4; if(r == 0){rday ++;} break;

case 3:rday = 90; r = fy %4; if(r == 0){rday ++;} break;

case 4:rday = 120; r = fy%4; if(r == 0){rday ++;} break;

案例5:rday = 151; r = fy%4; if(r == 0){rday ++;} break;

case 6:rday = 181; r = fy%4; if(r == 0){rday ++;} break;

case 7:rday = 212; r = fy%4; if(r == 0){rday ++;}休息;

案例8:rday = 243; r = fy%4; if(r == 0){rday ++;} break;

案例9:rday = 273 ; r = fy%4; if(r == 0){rday ++;} break;

case 10:rday = 304; r = fy%4; if(r == 0){rday ++ ;}打破;
案例11:rday = 334 ;; r = fy%4; if(r == 0){rday ++;} break;

}

rday = rday + sd;

tday = tday-rday; tday ++;

printf(\ nTotal Days:%d Days,tday);

getch();

}

include<stdio.h>



include<conio.h>



int main()
{
int fd,fm,fy,sd,sm,sy,ty,tday,rday,i,r,c1,c2;
printf("Entry Like Date<space>Month<space>Year\n");
printf("Enter Your 1st Date : ");
scanf("%d %d %d",&fd,&fm,&fy);
printf("Enter Your 2nd Date : ");
scanf("%d %d %d",&sd,&sm,&sy);
ty=fy-sy;
tday=ty*365;
tday--;
fy--;
for(i=sy;i<=fy;i++)
{
r=i%4;
if(r==0)
{
tday++;
}
}
c1=--fm;
switch(c1)
{
case 1:tday=tday+31;break;
case 2:tday=tday+59;fy++;r=fy%4;if(r==0){tday++;}break;
case 3:tday=tday+90;fy++;r=fy%4;if(r==0){tday++;}break;
case 4:tday=tday+120;fy++;r=fy%4;if(r==0){tday++;}break;
case 5:tday=tday+151;fy++;r=fy%4;if(r==0){tday++;}break;
case 6:tday=tday+181;fy++;r=fy%4;if(r==0){tday++;}break;
case 7:tday=tday+212;fy++;r=fy%4;if(r==0){tday++;}break;
case 8:tday=tday+243;fy++;r=fy%4;if(r==0){tday++;}break;
case 9:tday=tday+273;fy++;r=fy%4;if(r==0){tday++;}break;
case 10:tday=tday+304;fy++;r=fy%4;if(r==0){tday++;}break;
case 11:tday=tday+334;fy++;r=fy%4;if(r==0){tday++;}break;
}
tday=tday+fd;
c2=--sm;
switch(c2)
{
case 1:rday=31;break;
case 2:rday=59;r=sy%4;if(r==0){rday++;}break;
case 3:rday=90;r=fy%4;if(r==0){rday++;}break;
case 4:rday=120;r=fy%4;if(r==0){rday++;}break;
case 5:rday=151;r=fy%4;if(r==0){rday++;}break;
case 6:rday=181;r=fy%4;if(r==0){rday++;}break;
case 7:rday=212;r=fy%4;if(r==0){rday++;}break;
case 8:rday=243;r=fy%4;if(r==0){rday++;}break;
case 9:rday=273;r=fy%4;if(r==0){rday++;}break;
case 10:rday=304;r=fy%4;if(r==0){rday++;}break;
case 11:rday=334;;r=fy%4;if(r==0){rday++;}break;
}
rday=rday+sd;
tday=tday-rday;tday++;
printf("\nTotal Days : %d Days",tday);
getch();
}


#includestdio.h



int fy,fm,fd; //用于保存第一个日期的整数

int sy,sm,sd; //整数来保持第二个日期



/ *在此我假设第一个日期大于第二个日期* /



int oy,num_of_days,removed_days; //用于保存输出的整数



int i; //'for-loop'的变量

int c1; //第一个'开关'的变量

int c2; //第二个'开关'的变量



int ex; //退出序列



char line [100]; //输入行



int main(){

printf(请先选择模式\ n);

printf(输入1进行计算。\ n);

printf(输入0退出\ n);



printf(请输入以下oder \ n中的日期);

printf(****年****月**日** \ n \\ n');



而(1){



//模式或退出程序

printf(请输入你的需要:);

fgets(line,sizeof(line),stdin);

sscanf(line,%d,&ex);



//进行计算的表达式

if(ex = = 1){



//第一次约会

printf(请输入第一次约会:);

fgets(line,sizeof(line),stdin);

sscanf(行,%d%d%d,&fy,&fm,&fd);



//第二次约会

printf(请输入第二个日期:) ;

fgets(line,sizeof(line),stdin);

sscanf(行,%d%d%d,&sy,&sm,&sd); < br $> b $ b

oy = fy - sy; //取差多年

num_of_days = oy * 365; //设置输出天数



num_of_days--; //多花一天时间



if(fy!= sy)

fy--; //在first_year之前的最后一年





//在第一年和第二年之间取得闰年

for(i = sy; i< = fy; i ++){



if(i%4 == 0)

num_of_days ++ ;



}



if(fy!= sy)

fy ++ ; //将第一年的值设置为上一个值



//检查第一年是否为跳跃,如果是,则添加一天

if (fy%4)

num_of_days ++;



fm--;

c1 = fm;



//计算第一个月的天数

if(fm< 12){

switch(c1)

{

案例1:num_of_days + = 31; if(fy%4 == 0){num_of_days--;} break;

case 2:num_of_days + = 59;休息;

案例3:num_of_days + = 90;休息;

案例4:num_of_days + = 120;休息;

案例5:num_of_days + = 151;休息;

案例6:num_of_days + = 181;休息;

案例7:num_of_days + = 212;休息;

案例8:num_of_days + = 243;休息;

案例9:num_of_days + = 273;休息;

案例10:num_of_days + = 304;休息;

案例11:num_of_days + = 334;休息;

}

}



//无效日期抛出异常

else {

printf(错误:输入有效日期\ n);

继续;

}



num_of_days + = fd; //添加第一个日期的天数



c2 = --sm;



//计算第二个月的日子

if(sm <12){

开关(c2)

{

案例1:removed_days + = 31; if(sy%4 == 0){removed_days--;} break;

case 2:removed_days + = 59;休息;

案例3:removed_days + = 90;休息;

案例4:removed_days + = 120;休息;

案例5:removed_days + = 151;休息;

案例6:removed_days + = 181;休息;

案例7:removed_days + = 212;休息;

案例8:removed_days + = 243;休息;

案例9:removed_days + = 273;休息;

案例10:removed_days + = 304;休息;

案例11:removed_days + = 334;休息;



}

}

//抛出无效天数的例外情况

else {

printf(错误:输入有效日期\ n);

休息;

}

removed_days + = sd;



//检查第二年是否飞跃

if(sy%4 == 0)

removed_days ++;



//计算天数

num_of_days = num_of_days - removed_days;



printf(%d-%d-%d和%d-%d-%d.\之间有%d天,

num_of_days,fy,++ fm,fd,sy,++ sm,sd);

ex = 0; //更改退出状态

继续;

}



//退出

else if(ex == 0){



printf(感谢您使用此服务。\ n);

休息;



}



//表达错误模式

else {



printf(请输入有效的模式。\ n);

继续;



}



}





返回(0);

}
#include "stdio.h"

int fy,fm,fd; //integers to hold first date
int sy,sm,sd; //integers to hold second date

/*in this i have assumed that first date is greater than second date*/

int oy,num_of_days,removed_days; //integers to hold the outputs

int i; //variable for 'for-loop'
int c1; //variable for first 'switch'
int c2; //variable for second 'switch'

int ex; //the exit sequence

char line[100]; //the input line

int main(){
printf("Please select a mode first\n");
printf("Enter 1 to do calculations.\n");
printf("Enter 0 to exit\n");

printf("Please Enter the dates in following oder\n");
printf("****Year****month**date**\n\n");

while(1){

//taking the mode or exit the program
printf("Please enter your need : ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d",&ex);

//the expressions to do the calculations
if(ex == 1){

//taking the first date
printf("Please enter the first date : ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d %d %d",&fy,&fm,&fd);

//taking the second date
printf("Please enter the second date : ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%d %d %d",&sy,&sm,&sd);

oy = fy - sy; //taking the difference of the years
num_of_days = oy * 365; //setting output days

num_of_days--; //taking the extra day out

if(fy != sy)
fy--; //to take the last year before first_year


//taking the leap years between first and second year
for(i=sy;i<=fy;i++){

if(i%4 == 0)
num_of_days++;

}

if(fy != sy)
fy++; //setting first year value to previous value

//checking whether first year is leap, if it is then add one day
if(fy%4)
num_of_days++;

fm--;
c1 = fm;

//calculating the days of the first month
if(fm < 12){
switch(c1)
{
case 1 : num_of_days += 31; if(fy%4 == 0){num_of_days--;} break;
case 2 : num_of_days += 59; break;
case 3 : num_of_days += 90; break;
case 4 : num_of_days += 120; break;
case 5 : num_of_days += 151; break;
case 6 : num_of_days += 181; break;
case 7 : num_of_days += 212; break;
case 8 : num_of_days += 243; break;
case 9 : num_of_days += 273; break;
case 10 : num_of_days += 304; break;
case 11 : num_of_days += 334; break;
}
}

//throwing exception for invalid dates
else{
printf("Error : Enter valid date\n");
continue;
}

num_of_days += fd; //adding the days of the first date

c2 = --sm;

//calculating the days of the second month
if(sm < 12){
switch(c2)
{
case 1 : removed_days += 31; if(sy%4 == 0){removed_days--;} break;
case 2 : removed_days += 59; break;
case 3 : removed_days += 90; break;
case 4 : removed_days += 120; break;
case 5 : removed_days += 151; break;
case 6 : removed_days += 181; break;
case 7 : removed_days += 212; break;
case 8 : removed_days += 243; break;
case 9 : removed_days += 273; break;
case 10 : removed_days += 304; break;
case 11 : removed_days += 334; break;

}
}
//throwing exceptions for invalid days
else{
printf("Error : Enter valid date\n");
break;
}
removed_days += sd;

//checking whether second year is leap
if(sy%4 == 0)
removed_days++;

//calculating the number of days
num_of_days = num_of_days - removed_days;

printf("There are %d days between %d-%d-%d and %d-%d-%d.\n",
num_of_days,fy,++fm,fd,sy,++sm,sd);
ex = 0; //to change the exiting status
continue;
}

//the exit
else if(ex == 0){

printf("Thank you for using this service.\n");
break;

}

//the expression to eveluate wrong modes
else{

printf("Please enter a valid mode.\n");
continue;

}

}


return(0);
}


这篇关于如何使用c语言计算日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆