15,507,365 members
See more:
COleDateTimeSpan dtOleDuration;
CString m_sDuration;

here iam having two variables which is of different types now i want convert CString to ColeDateTimeSpan

this duration is between startdate and enddate of a batch / node
Posted
Jochen Arndt 25-Feb-15 10:39am
This depends on the format of the string (you should add an example to your question). The solution is simple if it is already a time span in the format [h]hh:mm:ss.

But if the time span consists of two date time strings it can be difficult. Then you have to split the string into the two parts and convert them to COleDateTime which can then be used to calculate the time span. This conversion depends on the used format and may fail if different formats are used.
Vamsi Krishna 26-Feb-15 1:43am
COleDateTimeSpan dtOleDuration;
CString m_sDuration;

m_sDuration= 36:22;
dtOleDuration=..............???????

How to convert this to Duration..........

write now iam calculating it by getting the difference between start date and end date like this

COleDateTime m_dtStopDate,m_dtStDate;

m_dtStDate.ParseDateTime(m_sStDate);
m_dtStopDate.ParseDateTime(m_sEndDate);

dtOleDuration=m_dtStopDate-m_dtStDate;

## Solution 1

You need to convert the string to an integer and use that integer to set the `ColeDateTimeSpan` variable, according to its definition.

## Solution 2

According to the above comments you seem to have a time span string in the format hh:mm:ss. Then you can perform the conversion like this:
```// Convert time span string to COleDateTimeSpan
// Format must be [[hh:]mm:]ss[.msec]
COleDateTimeSpan SpanFromString(const CString& strSpan) const
{
long lDays = 0;
long lHours = 0;
int nMins = 0;
int nSecs = 0;
LPCTSTR s1 = strSpan.GetString();
LPCTSTR s2 = _tcschr(s1, _T(':'));
LPCTSTR s3 = s2 ? _tcschr(s2 + 1, _T(':')) : NULL;
// hh:mm:ss
if (s3)
{
nSecs = _tstoi(s3 + 1);
nMins = _tstoi(s2 + 1);
lHours = _tstol(s1);
}
// mm:ss
else if (s2)
{
nSecs = _tstoi(s2 + 1);
nMins = _tstoi(s1);
}
// ss or empty string
else
nSecs = _tstoi(s1);
if (lHours >= 24)
{
lDays = lHours / 24;
lHours %= 24;
}
return COleDateTimeSpan(lDays, (int)lHours, nMins, nSecs);
}```

Vamsi Krishna 26-Feb-15 9:59am
i don't need to calculate all these stuff already i'm having the value in CString type...
just need to convert it to COleDateTimeSpan type..!!!!!
Jochen Arndt 26-Feb-15 10:18am
That is done by my code. The above function converts a CString containing something like "36:22" (interpreting it as minutes and seconds here) to COleDateTimeSpan. If that is not what you want you should state more precisely what you need.

From your comment I assumed that calling the function this way is what you want:
m_sDuration = "36:22";
dtOleDuration = SpanFromString(m_sDuration);