送分题(100分):如何将一个CString转成CTime?
CString str;
str="2003-6-4";
CTime t;
???请教:怎样将str赋给t呢?
问题点数:100、回复次数:6Top
1 楼ccwing(不是背书郎)回复于 2003-06-04 11:15:20 得分 10
为什么计算一下字符串。。。你这样认为简单了是不???
OK,下面有用牛刀杀鸡的办法。。。呵呵
首先
#include <comdef.h>
CString str;
_bstr_t bstr;
_variant_t vt;
COleDateTime olet;
str="2003-6-4";
bstr=str;
vt=bstr;
olet=vt;
CTime t(olet.GetYear(),olet.GetMonth(),olet.GetDay(),0,0,0);Top
2 楼greenever(司马青杉)回复于 2003-06-04 11:16:18 得分 30
笨办法。
把str中的年月日分别读出到三个字符串中,然后用atoi转换成整数,再用CTime的构造函数就行了CTime(Year,Month,Day,0,0,0)
算了,随手写一个给你:
CString strYear = str.Left(4);
CString strMonth = str.Mid(5,1);
CString strDay = str.Right(1);
/*注意,如果月、日的数字是两位数,就不行了。所以没有通用性。为了有通用性你可以先对字符串作处理,把月、日都加0补齐成两位数。*/
int nYear = atoi(strYear);
int nMonth = atoi(strMonth);
int nDay = atoi(strDay);
CTime t = CTime(nYear,nMonth,nDay,0,0,0);Top
3 楼pinghell(笑春风)回复于 2003-06-04 11:27:21 得分 20
int nYear,nMonth,nDay,nHour,nMin,nMiao;
nYear=?;
……
Ctime m_time=(nYear,nMonth,nDay,nHour,nMin,nMiao);Top
4 楼triggerd(有点着急)回复于 2003-06-04 11:56:01 得分 30
CString s="2003-06-4";
CString date[3];
date[0]=s.Mid(0,4);
date[1]=s.Mid(5,2);
date[2]=s.Mid(8,2);
int date2[3];
for(int i=0;i<3;i++)
date2[i]=atoi(date[i]);
CTime time(date2[0],date2[1],date2[2],0,0,0);
Top
5 楼free_card(痛并快乐着)回复于 2003-06-04 12:12:21 得分 10
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);
}
Top
6 楼Lipli()回复于 2003-06-04 13:36:46 得分 0
好~~好~~~好~~~给分~~~~Top




