You are here:

# C++/difference

Question
Please help me to write a C++ function named difDays() that calculates and returns the difference between two dates. Each date is
passed to the function as a structure using the following global type:
struct Date{
int month, day,year;
};

thanks! Godbless

Hi,

I have written the code below . It only works fine where the year is the same. if you want to use different year, you just have to count all the months from the month after the current month to the month before the said month. e.g 01/dec/2012- 02/Feb/2013 . there is 1 month between them. That is january. so, you have for december 31-1=30 days + january(31days) + 2 days all together  = 63. now subtract 1 (the starting point in december)

[code]

int divDate(Tdate* a, Tdate *b)
{
int monthsLeftA, monthsLeftB ;

const int ODD_MONTHS = 31;
const int EVEN_MONTHS = 30;
const int LEAPFEB = 29;
const int ODDFEB = 28;

if((a->month>4 || b->month>4)&&(a->month<6 || b->month<6))
{
ADate_days = a->day +( EVEN_MONTHS * 1) + (a->month-3)*ODD_MONTHS;
BDate_days =  b->day + (EVEN_MONTHS * 1) + (b->month-3)*ODD_MONTHS;

if(a->year%4==0 && b->year%4!=0)
{
BDate_days+=ODDFEB;
if(a->year>b->year)
else diff =  BDate_days - ADate_days;
}
else if(a->year%4!=0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else  if(a->year%4==0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else
{
BDate_days+=ODDFEB;
if(a->year>b->year)
}

}
else if((a->month>6 || b->month>6)&&(a->month<9 || b->month<9))
{
monthsLeftA = a->month-1;
monthsLeftA = (monthsLeftA-2)-1;
monthsLeftB = b->month-1;
monthsLeftB = (monthsLeftB-2)-1;

ADate_days = a->day +( EVEN_MONTHS * 2) + (monthsLeftA)*ODD_MONTHS;
BDate_days =  b->day + (EVEN_MONTHS * 2) + (monthsLeftB)*ODD_MONTHS;

if(a->year%4==0 && b->year%4!=0)
{
BDate_days+=ODDFEB;
if(a->year>b->year)

}
else if(a->year%4!=0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else  if(a->year%4==0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else
{
BDate_days+=ODDFEB;
if(a->year>b->year)
}
}

else if((a->month>9 || b->month>9)&&(a->month<11 || b->month<11))
{
monthsLeftA = a->month-1;
monthsLeftA = (monthsLeftA-3)-1;
monthsLeftB = b->month-1;
monthsLeftB = (monthsLeftB-3)-1;

ADate_days = a->day +( EVEN_MONTHS * 3) + (monthsLeftA)*ODD_MONTHS;
BDate_days =  b->day + (EVEN_MONTHS * 3) + (monthsLeftB)*ODD_MONTHS;

if(a->year%4==0 && b->year%4!=0)
{
BDate_days+=ODDFEB;
if(a->year>b->year)
}
else if(a->year%4!=0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else  if(a->year%4==0 && b->year%4==0)
{
BDate_days+=LEAPFEB;
if(a->year>b->year)
}
else
{
BDate_days+=ODDFEB;
if(a->year>b->year)
}

}

else
{
BDate_days =  b->day ;
if(a->day>b->day)
}

return diff;

}//end of divDate

[/code]
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Thank you very much!! the program you made is really help full.. thanks a lot! may the lord god bless you

C++

Volunteer

#### henry

##### Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

##### Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate