# C Programming (Turbo C++ Compiler) - Time Span Finding the number of days between two days

I have given here the source code for the time span.

The logic for finding leap year is applying the following two rules,

First Rule:
The year divisible by 4 is a leap year.
Second Rule: If the year is divisible by 100, then it is not a leap year. But If the year is divisible by 400, then it is a leap year.

## Source Code

#include <stdio.h>

#include <conio.h>

static int arrnumdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static int arrleapnumdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

typedef enum _boolean

{

true = 1, false = 0

} bool;

bool IsLeapYear(int year)

{

if((year % 4) == 0)

{

if((year % 100) == 0)

{

if( (year % 400) == 0)

return true;

else

return false;

}

else

return true;

}

return false;

}

bool CheckDate(int date, int month, int year)

{

if(year < 0 || year > 10000)

return false;

if(month < 1 || month > 12)

return false;

if(date < 1 || date > 31)

return false;

if(IsLeapYear(year) == true)

{

if( date > arrleapnumdays[month-1])

return false;

}

else

if( date > arrnumdays[month-1])

return false;

return true;

}

int GetNumDays(int begdate, int begmonth, int begyear, int enddate, int endmonth, int endyear)

{

int diffyears = endyear - begyear;

int numdays = 0;

int days = -1;

int m, y, d1, d2;

bool bLeap = false;

if(diffyears < 0)

return -1; // The start date is greater than end date

if( CheckDate(begdate, begmonth, begyear) == false)

return -2; // Not a valid start date

if(CheckDate(enddate, endmonth, endyear) == false)

return -3; // Not a valid end date

if(diffyears == 0)  // same year

{

int diffmonth = endmonth - begmonth;

if(diffmonth < 0)

return -1; // The start date is greater than end date

if(diffmonth == 0)

{

numdays = enddate - begdate;

if(numdays < 0)

return -1; // The start date is greater than end date

return numdays;

}

else

{

bLeap = IsLeapYear(begyear);

// Beg date of end of Beg month

if(bLeap == true)

days = arrleapnumdays[begmonth - 1];

else

days = arrnumdays[begmonth - 1];

numdays += days - begdate;

if(diffmonth > 1)

{

for(m = begmonth + 1; m <= endmonth - 1; m++)

{

if(bLeap == true)

numdays += arrleapnumdays[m - 1];

else

numdays += arrnumdays[m - 1];

}

}

// Beg of End month to End date

numdays += enddate;

}

}

else

{

// Beg Date to end of beg year (Dec 31, YYYY)

bLeap = IsLeapYear(begyear);

if(bLeap == true)

days = arrleapnumdays[begmonth - 1];

else

days = arrnumdays[begmonth - 1];

numdays += days - begdate;

for(d1 = begmonth + 1; d1 <= 12; d1++)

{

if(bLeap == true)

numdays += arrleapnumdays[d1 - 1];

else

numdays += arrnumdays[d1 - 1];

}

if(diffyears > 1)

{

for(y = begyear + 1; y <= endyear - 1; y++)

{

if(IsLeapYear(y) == true)

numdays += 366;

else

numdays += 365;

}

}

// Beg of End Year (Jan 01, YYYY) to End Date

bLeap = IsLeapYear(endyear);

for(d2 = 1; d2 <= endmonth - 1; d2++)

{

if(bLeap == true)

numdays += arrleapnumdays[d2 - 1];

else

numdays += arrnumdays[d2 - 1];

}

numdays += enddate;

}

return numdays;

}

int main()

{

int begdate, begmonth, begyear;

int enddate, endmonth, endyear;

char bd[128], ed[128];

int numdays;

printf("Enter Begin Date: ");

scanf("%d", &begdate);

printf("Enter Begin Month: ");

scanf("%d", &begmonth);

printf("Enter Begin Year: ");

scanf("%d", &begyear);

printf("Enter End Date: ");

scanf("%d", &enddate);

printf("Enter End Month: ");

scanf("%d", &endmonth);

printf("Enter End Year: ");

scanf("%d", &endyear);

sprintf(bd, "(DD/MM/YYYY) %02d/%02d/%04d", begdate, begmonth, begyear);

sprintf(ed, "%02d/%02d/%04d", enddate, endmonth, endyear);

numdays = GetNumDays(begdate, begmonth, begyear, enddate, endmonth, endyear);

if(numdays== -1)

printf("The start date is greater than end date\n");

else if(numdays == -2)

printf("Not a valid start date\n");

else if(numdays == -3)

printf("Not a valid end date\n");

else

printf("Number of days Between %s and %s is : %d\n", bd, ed, numdays);

return 0;

}

## Output

Enter Begin Date: 6

Enter Begin Month: 2

Enter Begin Year: 1978

Enter End Date: 1

Enter End Month: 11

Enter End Year: 2010

Number of days Between (DD/MM/YYYY) 06/02/1978 and 01/11/2010 is : 11956

Press any key to continue . . .

Enter Begin Date: 31

Enter Begin Month: 2

Enter Begin Year: 2010

Enter End Date: 10

Enter End Month: 10

Enter End Year: 2010

Not a valid start date

Press any key to continue . . .

Enter Begin Date: 10

Enter Begin Month: 10

Enter Begin Year: 2010

Enter End Date: 10

Enter End Month: 10

Enter End Year: 2000

The start date is greater than end date

Press any key to continue . . .