Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Can anybody help me with a C or C++ code. Requirement is as follows

I have a value of date in format "2011-07-01 09:26:53" and i want to compare it with the system time in windows and find the maximum between them

It will be a great help.

Thanks & Regards,
Lokesh
Posted

Hi,

You can do all of this with COleDateTime:

C++
CString szTime = _T("2010-07-01 09:26:53");
COleDateTime t;
if(t.ParseDateTime(szTime))
{
	t = t > COleDateTime::GetCurrentTime() ? t:COleDateTime::GetCurrentTime();
}
CString szMax = t.Format(_T("%A, %B %d, %Y"));
 
Share this answer
 
Comments
lokeshughade 5-Jul-11 5:47am    
Thanks a lot for your help.
test the program below and make your decision what to do....

C#
int _tmain(int argc, _TCHAR* argv[])
{
    char a[]="2011-01-12 05:12:30";
    char b[]="2001-01-12 05:12:30";
    int x=strcmp(a,b);
    x=strcmp(b,a);
    x=strcmp(a,a);
    return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 4-Jul-11 5:12am    
The question asks how to get the difference between the time in the string and the system time.
Mohibur Rashid 4-Jul-11 6:03am    
The Question Maker want the Maximum between two times. If he want the the time difference that is other thing.........
Richard MacCutchan 5-Jul-11 6:54am    
You are correct, my apologies.
lokeshughade 5-Jul-11 5:48am    
Thanks a lot for your help.
Use sscanf()[^] to parse the input and then convert it into a time value with mktime()[^]; something like:
struct tm newTime = {0};
PSTR pszTime = "2011-07-01 09:26:53";

sscanf(pszTime, "%4d-%2d-%2d %2d:%2d:%2d", &newTime.tm_year,
    &newTime.tm_mon,
    &newTime.tm_mday,
    &newTime.tm_hour,
    &newTime.tm_min,
    &newTime.tm_sec
time_t timeValue = mktime(&newTime);
time_t timeNow = time(NULL);
// compare the two values here
 
Share this answer
 
Comments
lokeshughade 5-Jul-11 5:47am    
Thanks a lot for your help.
Richard MacCutchan 5-Jul-11 5:56am    
Happy to help; don't forget to accept as solution any answers that work for you, and also to vote for the ones you really like. And, I don't mean just me, but also any of the other good people here.
lokeshughade 5-Jul-11 6:14am    
Hi Richard,

Thanks. I am actually new to the site so I was having no idea. Neways thanks a lot for your great help. Hope to get help from you in near future.

Regards,
Lokesh
I just copy - paste my code to get the difference between time. Hope it helps you.

//Get Creation Date into SystemTime
				SYSTEMTIME prCreateDateTimeSys;
				prCreateDateTimeSys.wYear = GetIntFromString(strCreationDate,4,0);
				prCreateDateTimeSys.wMonth = GetIntFromString(strCreationDate,2,4);
				prCreateDateTimeSys.wDay = GetIntFromString(strCreationDate,2,6);
				prCreateDateTimeSys.wHour = GetIntFromString(strCreationDate,2,8);
				prCreateDateTimeSys.wMinute = GetIntFromString(strCreationDate,2,10);
				prCreateDateTimeSys.wSecond = GetIntFromString(strCreationDate,2,12);
				prCreateDateTimeSys.wMilliseconds = 0;			//Not Used
				prCreateDateTimeSys.wDayOfWeek = 0;				//Not Used

				//Finding Difference between 2 DateTime
				COleDateTime prTimeCreated(prCreateDateTimeSys.wYear,prCreateDateTimeSys.wMonth,
					prCreateDateTimeSys.wDay,prCreateDateTimeSys.wHour,prCreateDateTimeSys.wMinute,
					prCreateDateTimeSys.wSecond);

				//Process End Date
				COleDateTime prEndDateTimeSys = COleDateTime::GetCurrentTime();
				
				
				COleDateTimeSpan timeDiff = prEndDateTimeSys - prTimeCreated;
 
Share this answer
 
Comments
lokeshughade 5-Jul-11 5:47am    
Thanks a lot for your help.
ShilpiP 5-Jul-11 6:15am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900