Difference between two dates with overloaded minus operator

I have to input two dates, then subtract the earlier date from the later date, and output the number of days that is the difference. So for example April, 18 2012 - April 10, 2012 would come out as 8 days difference.

I've spent the last few days searching for an answer to this problem, along with several other problems. I've gone through several online resources, but most suggest using the DateTime class, which I can't use for this exercise, or converting the integers into days using the Julian conversion formulas, but I can't figure out how to get that to work in the overloaded operator.

I am completely and thoroughly stumped, and if anyone could point me in the right direction or post a link that leads to some helpful tips on how to do this, I'd be greatly appreciative. I'll give what code I have for the operation.

1
2
3
4
5
6
7
8
9
10
Date Date::operator - (const Date &right)
{
	Date temp;

	temp.year = year - right.year;
	temp.month = month - right.month;
	temp.day = day - right.day;
	temp    
	return temp;
}


1
2
3
4
5
6
7
8
9
10
11
void Date::dateDiff()
{
	int daysinyear[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};

	int days;
	days = year * 365 + (year / 4);
	days = days + daysinyear[month-1];
	days = days + day;
	
	return;
}


And this is how I'm trying to get this to work in main, but my << is overloaded to give a certain date format, so outputting Date3 this way doesn't work, because I'm trying to output an integer, but I don't know of any other way to do it.

1
2
3
4
 cout << "The difference between the two dates is: " << endl;

	 Date3 = Date1 - Date2;
	 cout << Date3;
Well, the way you've written your Date::operator-(), it's never going to work.
For example, suppose A="2012-01-01" and B="2012-02-01". By your definition, B-A="0000-01-00", which would mean the two dates are 31 days apart.
However, suppose now that A="2012-02-01" and B="2012-03-01". B-A="0000-01-00" still, even though the dates are actually 29 days apart.
And that's not even getting into other cases like "2012-03-01"-"2012-02-20".

Date::operator-() should return an int. Convert each of the operands to they Julian days and return the difference of these integers.

Note that the expression (year * 365 + (year / 4)) is incorrect by our current standards of time. This implies that a year has 365.25 days (Julian calendar), when in fact it has 365.2425 days (Gregorian calendar).
1
2
3
4
5
6
7
8
9
10
int Date::operator - (const Date &right)
{
	int jDate1;
	int jDate2;
	int newDate;
	jDate1 = (1461 * (year + 4800 + (month - 14)/12))/4 +(367 * (month - 2 - 12 * ((month - 14)/12)))/12 - (3 * ((year + 4900 + (month - 14)/12)/100))/4 + day - 32075;
	jDate2 = (1461 * (right.year + 4800 + (right.month - 14)/12))/4 +(367 * (right.month - 2 - 12 * ((right.month - 14)/12)))/12 - (3 * ((right.year + 4900 + (right.month - 14)/12)/100))/4 + right.day - 32075;
	newDate = jDate1 - jDate2;
	return newDate;
}


All right, so I found a Julian conversion formula that I think will work, and I've built it in my overloaded operator, and tried my best with my limited knowledge of how to use overloaded operators. The compiler runs it, but when I get to the part where two dates are subtracted, I get a bunch of unhandled exceptions.

Is this close to what you were talking about, or am I still way off?
part where two dates are subtracted, I get a bunch of unhandled exceptions.


The actual subtracting of 2 integers is throwing a bunch of unhandled exceptions? Are you sure? What are the exceptions? Post verbatim.
Okay, I ran it again and there were no unhandled exceptions, but the output for the interger subtraction is garbage. Outputs like this:

The difference between the two dates is January -858993460-858993460

And I know what's causing it, my overloaded << operator looks like this.

1
2
3
4
5
ostream &operator << (ostream &strm, const Date &obj)
{	
	strm << obj.monthName() << "," << obj.day << obj.year;
	return strm;
}


The << operator has to output a date in that format, it's a requirement of this assignment. I'm not sure how to get around that. Could I maybe create a sub-class to handle the date subtraction and put another overloaded operator in it specifically to output the integer I get from the subtraction of the Julian date values?
but the output for the interger subtraction is garbage. Outputs like this:
And I know what's causing it, my overloaded << operator looks like this.
What do you mean, are you saying you believe your << has something to do with the garbage date? If so, that is not the case.

What is wrong with your ostream operator<<?

I'll try running your code.
Topic archived. No new replies allowed.