Re: Comparing DateTime ?
Thanks for the reply
here 's the code for comparing 2 dates
int hour = 0;
int min = 0;
int sec = 0;
int Month=0;
int Day = 0;
int Year = 0;
std::string strFirstDate="23:34:22 1 21 2008";// (HH:MM:SS Month
Date Year )
std::string strSecondDate="23:34:22 6 12 2007";
time_t rawtime;
struct tm * FirstTime;
struct tm * SecondTime;
time ( &rawtime );
sscanf(strFirstDate.c_str(), "%d:%d:%d %d %d %d", &hour, &min, &sec,
&Month, &Day, &Year);
FirstTime = localtime ( &rawtime );
FirstTime->tm_hour = hour;
FirstTime->tm_min = min;
FirstTime->tm_sec = sec;
FirstTime->tm_mon = Month;
FirstTime->tm_mday = Day;
FirstTime->tm_wday = ( Year - 1900 );
time_t newTime = mktime(FirstTime );
time ( &rawtime );
SecondTime = localtime ( &rawtime );
sscanf(strSecondDate.c_str(), "%d:%d:%d %d %d %d", &hour, &min,
&sec, &Month, &Day, &Year);
SecondTime->tm_hour = hour;
SecondTime->tm_min = min;
SecondTime->tm_sec = sec;
SecondTime->tm_mon = Month;
SecondTime->tm_mday = Day;
SecondTime->tm_wday = ( Year - 1900 );
time_t existingTime = mktime(SecondTime);
if (difftime(newTime,existingTime) > 0)
{
MessageBox(_T("First date is greater then Second Date"));
}
else if (difftime(newTime,existingTime) == 0)
{
MessageBox(_T("First date and Second Date are equal"));
}
else if (difftime(newTime,existingTime) < 0)
{
MessageBox(_T("First date is smaller then Second Date"));
}
well I tried the same thing with ColeDateTime .... and it worked fine
for me .....
But was not able to figure out y the above code failed in comparing 2
datetime
On Feb 2, 2:32 pm, David Lowndes <Dav...@example.invalid> wrote:
I get the date and time in the format ex : 10:45:44 Jan 2 2008 and
9:23:15 Nov 12 2007
and pass it to a structure of type tm , I also convert month as
number .
struct tm * time1;
use time_t newTime = mktime(time1 );
and difftime for comparing .........
Real compilable code please. Ideally a short console application that
demonstrates the issue.
Dave