# Thread: Review required for program of date difference

1. ## Review required for program of date difference

hello,
i created a simple program to calculate date difference. Please review it and suggest optimisations, if any.

Code:
```#include <stdio.h>
#include <math.h>

void cal_date_diff(int day1,int month1,int year1,int day2,int month2,int year2)
{
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //respective days in month
int d,i=month1+1; //initialising date difference and i
if(month1==2 && ((year1 &#37; 4==0 || year1 % 100==0) && year1 % 400!=0))
d=29-day1;
else
d=m[month1]-day1;

if(year1==year2 && month1==month2)
{
printf("The Difference is %d",abs(day1-day2));
return;
}
if(i==13)
i=1;
while(1)
{
while(i<=12)
{
if(year1==year2 && i==month2) //calculation over since both years same
//and months too
{
d+=day2;
printf("The Difference is %d",d);
return;
}
if(i==2 && ((year1 % 4==0 || year1 % 100==0) && year1 % 400!=0))
//leap year so count 29 days
d+=29;
else
d+=m[i];
i++;
}
i=1;
year1++;
}
over:

}

int main(void)
{
int day1,day2,month1,month2,year1,year2;
day1=30; month1=7; year1=1988;
day2=22; month2=8; year2=1988;
if(year1>year2)
cal_date_diff(day2,month2,year2,day1,month1,year1);
else if(year2>year1)
cal_date_diff(day1,month1,year1,day2,month2,year2);
else
{
if(month1>month2)
cal_date_diff(day2,month2,year2,day1,month1,year1);
else
cal_date_diff(day1,month1,year1,day2,month2,year2);
}
return 0;
}```

2. First, there is absolutely no need for a "goto". Instead, why not do this:

Code:
```		if(year1==year2 && i==month2)
{
d+=day2;
printf("The Difference is %d",d);
}```

3. Or, perhaps, replace "goto over" with "break" which is less obviously a goto.

But the real solution is to change the conditions on the while-loop in such a way that you exit when the conditions are true. E.g add the "month2 == i" on the inner while-loop.

Code:
`d=m[month1]-day1`
I think this can go wrong if you are in a leap-year...

Code:
`		int d=abs(day1-day2); //only difference in days so directly print`
Should that not be part of the date-difference function instead?

--
Mats

4. I cannot write "month2 == i in the inner while-loop" because example if i enter 1st date as 22 6 1988 and 2nd date as 22 7 1989 then the inner loop won't get executed at all and it will increment the year without counting the days of july..december of 1988 and hence wrong answer.

Any more suggestions? and please check it whether it produces correct answer or not

5. Originally Posted by chottachatri
I cannot write "month2 == i in the inner while-loop" because example if i enter 1st date as 22 6 1988 and 2nd date as 22 7 1989 then the inner loop won't get executed at all and it will increment the year without counting the days of july..december of 1988 and hence wrong answer.
So add the "year1 == year2" to the condition? Goto is NOT the right answer here.

Any more suggestions? and please check it whether it produces correct answer or not
So, you are suggesting that I re-implement your code in a different form, validate it, and check that your code produces the correct result, or do you have any other suggestion for validating your code - isn't that YOUR task?

Code:
``` if(month1==2)
d=29-day1;
else
d=m[month1]-day1;```
You need to check if it's a leapyear or not for february calculation - your current code assumes it is ALWAYS a leap-year. I personally would write a small function that produces "days in month" based on year and month.

--
Mats

6. Oops...! i wrote it by mistake in hurry to correct it. I'll modify it

7. Why not use time.h to perform these comparisons?