如何把CString数据类型转换为CTime数据类型。

解决方案 »

  1.   

    使用COleDateTime 吧。 有个ParseDateTime可以实现这个功能。 
    或者你先用COleDateTime然后转换为CTime
      

  2.   

    CTime不是有个内部函数吗?CString Format( LPCTSTR pFormat ) const
      

  3.   

    不好意思,理解错误,看来真的得用COleDateTime呢
      

  4.   

    我以前用了一个笨的方法:分别获取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;
    }
    见笑了
      

  5.   

    这有一个非常简单的函数,您可以在此上面加入你所需要的:
    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);
    }