Click here to Skip to main content
15,851,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
ridoy 18-Aug-12 7:52am    
Seems to be homework..

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();
}
 
Share this answer
 
Comments
CHill60 7-Apr-15 3:18am    
The question is over 2 years old and is already answered.
There are plenty of "we can do your homework for you" sites out there, this is not one of them.
Member 15572144 19-Mar-22 18:29pm    
Thanks for this man, it helped me a lot. i changed a lot of things but your template helped me to understand how to work thing. i understood the switch function thanks to you. thank you for your help.
#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);
}
 
Share this answer
 
Comments
CHill60 10-Jun-15 7:25am    
The question is over 2 years old and is already answered.
There are plenty of "we can do your homework for you" sites out there, this is not one of them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900