i'm working on a library system, and i need both current date (date of the loan) and the date 4 weeks from now (that day one is to return a book), and return them both as strings.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
time_t rawtime;
struct tm* timeinfo;
string today;
string due_date;
char* buf=newchar [1024];
// Get the number of seconds since 1 January, 1970
rawtime=time(NULL);
// Convert to a local time and date
timeinfo=localtime(&rawtime);
// Convert to string
strftime(buf,1024,"%A, %d %B %Y",timeinfo);
today=buf;
delete [] buf;
buf=newchar [1024];
// Add 4 weeks
rawtime+=4*(7*86400);
// Convert to local time and date
timeinfo=localtime(&rawtime);
// Convert to string
strftime(buf,1024,"%A, %d %B %Y",timeinfo);
due_date=buf;
cout << "Today is: " << today;
cout << endl;
cout << "Your book(s) are due back on: " << due_date;
cout << endl;
system("pause");
}
Basically this takes the current time and turns it into a date string, then adds 2419200 seconds (4 weeks worth) and turns that into a date.