# How to add Days to a Date?

• 03-20-2002
Bufferlo
How to add Days to a Date?
I want my program to have a date input in the format of (dd, mm, yyyy), then another input in terms of the number of day(just an integer). And then I want my programm to have an out put of another date (in form of dd, mm, yyyy again). The algorithm is something like: Output_Date = Input_Date + days. I cannot find a simple algorithm.
Can anyone helps? Thanks.
• 03-20-2002
Esparno
Quote:

The algorithm is something like: Output_Date = Input_Date + days. I cannot find a simple algorithm.
uh...that is the algorithm, all you have to do is slap a ; at the end and you will be all set, oh, and make the cin statements, i ASSume u know how to do that.... but u know what happens when u ASS-U-ME
• 03-20-2002
Bufferlo
Furthermore, I don't want to use Julian Days.
Can anyone use Georgian Calenar to formulate an relatively easy algorithm?
• 03-20-2002
If it was simple you'd probably be able to figure it out or have found it yourself since you know there is a difference between the Julian and Georgian calendar. I suggest you use the search button located in the upper right hand corner of this screen to do a search of the board using calendar as the search key. I am confident you will find a number of pertinent posts. Together with whatever other resources you have available, use them as you will to develop the best algorthym you can devise. Then post your work with pertinent questions, error codes, etc..
• 03-20-2002
Unregistered
This solution doesn't check input, and I don't have anything to test it with. It follows the Gregorian rules I found here:

http://www.genealogy.org/~scottlee/cal-overview.html

and I wrote it with MSVC++6. If you're using say GCC, change _snprintf to snprintf.

Code:

```#include <iostream> #include <cstdio> using namespace std; // this follows Gregorian rules bool isLeapYear(int year) {         if (year%400==0) return true;         if (year%100==0) return false;         return (year%4==0); } int main() {         int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };         int day,month,year, daysToAdvance;         char line[81];         cout << "Enter date (dd, mm, yyyy): ";         cin.getline(line, 80);         sscanf(line, "(%i, %i, %i)", &day, &month, &year);         cout << "Enter days to advance: ";         cin >> daysToAdvance;         for (int i=0;i<daysToAdvance;i++)         {                 day++;                 if (day > daysPerMonth[month-1] || ( month==2 && day==29 && !isLeapYear(year) ) )                 {                         day = 1;                         month++;                         if (month==13)                         {                                 month = 1;                                 year++;                         }                 }         }         char output[80];         _snprintf(output, 80, "(%02i, %02i, %04i)", day, month, year);         cout << "Result: " << output << endl;         fflush(stdin);         getchar();         return 0; }```