Although Java is my forte, I started my early days by coding with C. The below program is to get the difference in days between two dates. The program is simple and has well defined comments to explain for itself. Thought I would post it here.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int datecmp(struct date,struct date);
int leap(int);
void main()
{
struct date a,b;
int leep;
clrscr();
b.da_year=2000;
b.da_mon=7;
b.da_day=1;
getdate(&a);
printf("%u\t%u\t%u\t",b.da_day,b.da_mon,b.da_year);
printf("\n%u\t%u\t%u\t",a.da_day,a.da_mon,a.da_year);
datecmp(a,b);
/*if((leep=leap(b.da_year))==1)
printf("\nThe year is leep year");
else
printf("\nThe year is an ordinary year");*/
getch();
}
int datecmp(struct date a,struct date b)
{
struct date c;
char days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int y=365;
int i,j;
int td=0;
c.da_year=a.da_year-b.da_year;
c.da_mon=a.da_mon-b.da_mon;
if(c.da_year==0)//SAME YEAR BUT DIFFERENT MONTH OR SAME MONTH
{
if(c.da_mon==0)
td=a.da_day-b.da_day;
else
{
for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
td+=days[j];
td+=days[b.da_mon-1]-b.da_day;
td+=a.da_day-1;
}
}
else if(c.da_year>1 && c.da_mon>1)//YEAR DIFF B/W TWO INPUTS IS >THAN 1 YEAR
{
if(b.da_mon<3 && leap(b.da_year)==1)
td=1;
else
td=0;
for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
td=td+365+leap(j);
if(c.da_mon==0)
td+=a.da_day-b.da_day;
else
{
for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
td+=days[j];
td+=days[b.da_mon-1]-b.da_day;
td+=a.da_day-1+leap(a.da_year);
}
}
else if(c.da_year==1)//YEAR DIFF B/W TWO INPUTS IS==1 YEAR
{
if(c.da_mon>0)
{
if(b.da_mon>1 && leap(b.da_year)==1)
td=1;
else
td=0;
for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
td=td+365+leap(j);
if(c.da_mon==0)
td+=a.da_day-b.da_day;
else
{
for(i=0,j=b.da_mon;i<(c.da_mon-1);i++,j++)
td+=days[j];
td+=days[b.da_mon-1]-b.da_day;
td+=a.da_day-1+leap(a.da_year);
}
}
else if(c.da_mon==0 && c.da_day>0)
{
if(b.da_mon>1)
td=0;
else
td=1;
for(i=0,j=b.da_year+1;i<(c.da_year);i++,j++)
td=td+365+leap(j);
td+=a.da_day-b.da_day;
}
}
else if(c.da_year==1)
{
}
printf("\n The days are %d",td);
getch();
}
int leap(int year)//LEAP YEAR FUNCTION
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
return(1);
else
return(0);
}
else
return(1);
}
else
return(0);
}
Cheers,
Bragaadeesh.