Adding days to a date

We were given an assignment to add a number of days to a date. So I would add for example 10 days to Jan 20, 2013. I understand operator overloading and I an ignoring the leap year issue for now, what I can't figure out is the logic needed to add a certain numbers of days to a specified date.

I've done programs in the past where you add hours and minutes, but this is easy because an hour is always 60 mins and a minute is always 60 seconds, so you can easily use % 60. But with months all the days are different.

Would it make sense to correspond every day with a certain number, then add the numbers and convert it back to a date? For example, Jan 1 is the first day of the year, so if I add 32 to Jan 1 I get the 32nd day of the year which is Feb 1. (if it's not a leap year). And I would convert 32 to Feb 1? I'm not sure how I would do it, but do you think that would make sense for an algorithm or am I totally overthinking this?

I'm sure there is already pre-written templates for this, but we have not been taught those so it needs to be done without using any library or template.

If you want to it all manually you could create a Date class that contains a Month class, Year class, and an int for days.
Last edited on
Im new to this so I probably will not be any help but if jan is 1-31 and feb is 32-58 than: Jan 20 + 30=50, 50 - 31 = 19 the result would be feb 19 which you might be able to do with an if or while statement.

if (ans>31) && (ans<58)
cout << "Feb" << ans
- first I will convert (day,month,year) (Gregorian) to Julian day number https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_Day_Number

- then add days to Julian day number

- finally convert Julian day number back to (day,month,year) (Gregorian) https://en.wikipedia.org/wiki/Julian_day#Gregorian_calendar_from_Julian_day_number

1
2
long toJdn(int day, int month, int year);
void toCalendarDate(long jdn, int& day, int& month, int& year);


it's more complicate than if...else... but it is funnier this way
Topic archived. No new replies allowed.