Link to home
Start Free TrialLog in
Avatar of anine
anine

asked on

Getting right time....

Does anyone have som code that shows how to get todays date (just date, month and year) and store it in a variable, fra_dato. And then add 1 month to todays date and store it in another variable, til_dato??
And then compare those 2 variables.
All variables must be shown on the output. I'm having trouble getting it set up correctly.

#include <iostream.h>
#include <afx.h>

class time()
{
         ???    til_dato;
         ???    fra_dato;
public:

}

void main()
{
CTime fra_dato = CTime::GetCurrentTime();
//To make a time 1 month away:

CTime til_dato = CTime::GetCurrentTime();
int yr = til_dato.GetYear();
int mo = til_dato.GetMonth();
int da = til_dato.GetDay();
int hr = til_dato.GetHour();
int mi = til_dato.GetMinute();
int sec = til_dato.GetSecond();
if(++mo > 12){
         yr++;
}
CTime t3 = CTime(yr, mo, da, hr, mi, sec, -1);
}
Avatar of nietod
nietod

time_t CurTim = time(NULL);
tm CurrentTime = localtime(CurTim);
tm NextTime = CurrentTime;

++NextTime..tm_mon; // Increment month.
if (NextTime.tm_mon > 11) // If month is invalid.
{
   NextTime.tm_mon = 0; Reset to January.
   ++NextTime.tm_year; // of next year.
};


Now one problem is what do you do with days that are not valid.  Like what if today is the 31st and the next month doesn't have a 31is?  should it be the 30th of the next month?  Or the 1st of the month after that?  Or something else.
Avatar of anine

ASKER

The answer lacks information about how you write to the screen.

just cout the infrormation.

I don't know how you want it to look.  But something like

cout << CurrentTime.tm_mon << '/' <<CurrentTime.tm_mday << '/' << CurrentTime.tm_year + 1900 << endl;

It s not necesssary to reject an answer if you just want additional help or clarification.  Most problems are solved by a "dialog" between an expert and the client.  
Avatar of anine

ASKER

Adjusted points from 75 to 100
Avatar of anine

ASKER

Sorry, I'm new at this ;-)
Thank you very much!!
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anine

ASKER

Yes, it was acceptable!
Thanx.