Visual C++ - 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

// LeapYear.cpp : Defines the entry point for the console application.

//

#include <iostream>

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 };

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;

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(int 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(int d1 = begmonth + 1; d1 <= 12; d1++)

{

if(bLeap == true)

numdays += arrleapnumdays[d1 - 1];

else

numdays += arrnumdays[d1 - 1];

}

if(diffyears > 1)

{

for(int 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(int 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;

std::cout << "Enter Begin Date: ";

std::cin >> begdate;

std::cout << "Enter Begin Month: ";

std::cin >> begmonth;

std::cout << "Enter Begin Year: ";

std::cin >> begyear;

std::cout << "Enter End Date: ";

std::cin >> enddate;

std::cout << "Enter End Month: ";

std::cin >> endmonth;

std::cout << "Enter End Year: ";

std::cin >> endyear;

char bd[128], ed[128];

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

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

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

if(numdays== -1)

std::cout << "The start date is greater than end date\n";

else if(numdays == -2)

std::cout << "Not a valid start date\n";

else if(numdays == -3)

std::cout << "Not a valid end date\n";

else

std::cout << "Number of days Between " << bd << " and " << ed << " is : " << numdays << "\n";

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 . . .