方法1、TDateTime强制转换字符串
AnsiString sDate;
int second,minute,hour,day,month,year;
second=10;
minute=10;
hour=10:
day=30;
month=10;
year=16;
sDate.sprintf("%d-%d-%d %d:%d:%d",year,month,day,hour,minute,second);
return TDateTime(sDate);
方法2、StrToDateTime
TDateTime myDt=StrToDateTime("2012-03-05 08:12:12");
注意看自己系统时间格式,倘若不是这个格式会报错,在控制面板中修改为相应的格式。不想修改系统时间格式的话,可以用方法3自己构造TDateTime,这样原来字符串是什么样的都行。
方法3、构造TDateTime
TDateTime TForm1::MyStrToDateTime(String strDateTime)
{
unsigned short nYear = 0,nMonth = 0,nDay = 0;
unsigned short nHour = 0,nMin = 0,nSec = 0;
/*
** 提取时间数据
*/
nYear = strDateTime.SubString(1,4).ToIntDef(1990);
nMonth = strDateTime.SubString(6,2).ToIntDef(1);
nDay = strDateTime.SubString(9,2).ToIntDef(1);
nHour = strDateTime.SubString(12,2).ToIntDef(0);
nMin = strDateTime.SubString(15,2).ToIntDef(0);
nSec = strDateTime.SubString(18,2).ToIntDef(0);
/*
** 构造时间
*/
TDateTime clDateTime(nYear,nMonth,nDay);
ShowMsg(clDateTime.FormatString("hh:mm:ss"));
clDateTime += TDateTime(nHour,nMin,nSec,0);
return clDateTime;
}