如何把CString类型转换为CTime类型。 如何把CString数据类型转换为CTime数据类型。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用COleDateTime 吧。 有个ParseDateTime可以实现这个功能。 或者你先用COleDateTime然后转换为CTime CTime不是有个内部函数吗?CString Format( LPCTSTR pFormat ) const 不好意思,理解错误,看来真的得用COleDateTime呢 我以前用了一个笨的方法:分别获取CString里的年月日,把它们转换成int,然后再转换成CTime。例如:CString strDate = _T("2003-04-23");int iYear, iMonth, iDay;iYear = StrToIntAt(strDate, 0, 3);iMonth = StrToIntAt(strDate, 5, 6);iDay = StrToIntAt(strDate, 7, 8);CTime time = CTime(iYear, iMonth, iDay, 0, 0, 0);int StrToIntAt(CString string, int startPos, int endPos){ CString strChange = _T(""); char c_change; for (int i=startPos; i<=endPos; i++) { c_change = string.GetAt (i); strChange += c_change; } int iresult = (int)atof(strChange); return iresult;}见笑了 这有一个非常简单的函数,您可以在此上面加入你所需要的:BOOL ScanTime(Ctime &time, // o - filled in time structureLPCTSTR lpszTime, // I - the string containing the time tobe extractedLPCTSTR lpszFormat // I - the time format to extractaccording to){int nYear = 1980; // extracted time fieldsint nMonth = 1;int nDay = 1;int nHour = 0;int nMin = 0;int nSec = 0;int nFlag = DATE_TIME; // DATE_TIME / DATE_ONLY / TIME_ONLYCstring msg;// start at the beginningchar *pTime = (char *)lpszTime;char *pFmt = (char *)lpszFormat;while (*pFmt != '\0'){if (*pFmt == '%'){pFmt++;switch (*pFmt){case 'Y' : // year with centurysscanf (pTime,"%4d",&nYear);if (nYear <1980 || nYear> 2036){msg.Format ("Invalid year (%d)",nYear);AfxMessageBox (msg);return (FALSE);}pTime+=4;break;case 'y' : // year without century (00-99)sscanf (pTime,"%2d",&nYear);nYear = nYear + (nYear > 36 ? 1900 : 2000);if (nYear <1980 || nYear> 2036){msg.Format ("Invalid year (%d)",nYear);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;case 'm' : // month (01-12)sscanf (pTime,"%2d",&nMonth);if (nMonth <1 || nMonth> 12){msg.Format ("Invalid month (%d)",nMonth);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;case 'd' : // day of month (01-31)sscanf (pTime,"%2d",&nDay);if (nDay <1 || nDay> 31){msg.Format ("Invalid day (%d)",nDay);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;case 'H' : // hour (00-23)sscanf (pTime,"%2d",&nHour);if (nHour <0 || nHour> 23){msg.Format ("Invalid hour (%d)",nHour);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;case 'M' : // minute (00-59)sscanf (pTime,"%2d",&nMin);if (nMin <0 || nMin> 59){msg.Format ("Invalid minute (%d)",nMin);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;case 'S' : // second (00-59)sscanf (pTime,"%2d",&nSec);if (nSec <1 || nSec> 31){msg.Format ("Invalid second (%d)",nSec);AfxMessageBox (msg);return (FALSE);}pTime+=2;break;default :msg.Format("Invalid format specifier (%c)",*pFmt);AfxMessageBox(msg);return (FALSE);break;}}else{if (!isdigit((int)(*pTime))){if (*pTime == *pFmt){pTime++;}else{msg.Format ("Character mismatch : Expected %c, found%c",*pFmt,*pTime);AfxMessageBox ((LPCTSTR)msg);return (FALSE);}}}pFmt++;}// successtime = Ctime(nYear,nMonth,nDay,nHour,nMin,nSec);return (TRUE);} 怎样才能禁止界面拉伸呢? 全局钩子WH_MOUSE_LL导致调用钩子的对话框变慢 老生常谈-背景问题 请问这样用会不会有漏洞? 那款摄像头软件比较好? 100分求国内邮箱地址列表~~~~~~~~~~ 如何在vc中读取中文字符并能用来判断 MFC单文档对话框加入第三方控件就弹不出来了 请您来坐坐 怎样在基于对话框程序中加一个最小化的按钮,在X左侧 大家看看能否解决这个问题。一个对我来说有些挑战的问题。 如何在运行时确定对话框的大小呢。我已经问的没有分了,还没人回答,惨
或者你先用COleDateTime然后转换为CTime
CString strDate = _T("2003-04-23");
int iYear, iMonth, iDay;
iYear = StrToIntAt(strDate, 0, 3);
iMonth = StrToIntAt(strDate, 5, 6);
iDay = StrToIntAt(strDate, 7, 8);
CTime time = CTime(iYear, iMonth, iDay, 0, 0, 0);int StrToIntAt(CString string, int startPos, int endPos)
{
CString strChange = _T("");
char c_change;
for (int i=startPos; i<=endPos; i++)
{
c_change = string.GetAt (i);
strChange += c_change;
}
int iresult = (int)atof(strChange);
return iresult;
}
见笑了
BOOL ScanTime
(
Ctime &time, // o - filled in time structure
LPCTSTR lpszTime, // I - the string containing the time to
be extracted
LPCTSTR lpszFormat // I - the time format to extract
according to
)
{
int nYear = 1980; // extracted time fields
int nMonth = 1;
int nDay = 1;
int nHour = 0;
int nMin = 0;
int nSec = 0;
int nFlag = DATE_TIME; // DATE_TIME / DATE_ONLY / TIME_ONLY
Cstring msg;// start at the beginning
char *pTime = (char *)lpszTime;
char *pFmt = (char *)lpszFormat;
while (*pFmt != '\0')
{
if (*pFmt == '%')
{
pFmt++;
switch (*pFmt)
{
case 'Y' : // year with century
sscanf (pTime,"%4d",&nYear);
if (nYear <1980 || nYear> 2036)
{
msg.Format ("Invalid year (%d)",nYear);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=4;
break;
case 'y' : // year without century (00-99)
sscanf (pTime,"%2d",&nYear);
nYear = nYear + (nYear > 36 ? 1900 : 2000);
if (nYear <1980 || nYear> 2036)
{
msg.Format ("Invalid year (%d)",nYear);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
case 'm' : // month (01-12)
sscanf (pTime,"%2d",&nMonth);
if (nMonth <1 || nMonth> 12)
{
msg.Format ("Invalid month (%d)",nMonth);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
case 'd' : // day of month (01-31)
sscanf (pTime,"%2d",&nDay);
if (nDay <1 || nDay> 31)
{
msg.Format ("Invalid day (%d)",nDay);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
case 'H' : // hour (00-23)
sscanf (pTime,"%2d",&nHour);
if (nHour <0 || nHour> 23)
{
msg.Format ("Invalid hour (%d)",nHour);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
case 'M' : // minute (00-59)
sscanf (pTime,"%2d",&nMin);
if (nMin <0 || nMin> 59)
{
msg.Format ("Invalid minute (%d)",nMin);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
case 'S' : // second (00-59)
sscanf (pTime,"%2d",&nSec);
if (nSec <1 || nSec> 31)
{
msg.Format ("Invalid second (%d)",nSec);
AfxMessageBox (msg);
return (FALSE);
}
pTime+=2;
break;
default :
msg.Format("Invalid format specifier (%c)",*pFmt);
AfxMessageBox(msg);
return (FALSE);
break;
}
}
else
{
if (!isdigit((int)(*pTime)))
{
if (*pTime == *pFmt)
{
pTime++;
}
else
{
msg.Format ("Character mismatch : Expected %c, found
%c",*pFmt,*pTime);
AfxMessageBox ((LPCTSTR)msg);
return (FALSE);
}
}
}
pFmt++;
}
// success
time = Ctime(nYear,nMonth,nDay,nHour,nMin,nSec);
return (TRUE);
}