上边的都对,再给一个参考: function StringToDatetime(DateStr:string):TDateTime; //将yyyy-mm-dd hh:nn:ss格式的字符串转为时间格式 var fs:TFormatSettings; begin fs.DateSeparator:='-'; fs.TimeSeparator:=':'; fs.ShortDateFormat:='yyyy-mm-dd'; fs.ShortTimeFormat:='hh:nn'; Result:=StrToDateTimeDef(DateStr,0,fs); end;
str1:=Copy(str,1,4);
str2:=Copy(str,5,2);
str3:=Copy(str,7,2);
str4:=str1+'-'+str2+'-'+str3;
date:=StrToDate(str4);
begin
Result:=StrToDateTime(Format('%s-%s-%s', [copy(dt,1,4), copy(dt, 5, 2), copy(dt, 7,2)]));
end;
DateTimeToStr 可以将日期格式转换为字符串
这两个函数基本就可以搞定你的问题,如果还需要特殊处理则用copy函数即可
strtodatetime('')
function StringToDatetime(DateStr:string):TDateTime; //将yyyy-mm-dd hh:nn:ss格式的字符串转为时间格式
var
fs:TFormatSettings;
begin
fs.DateSeparator:='-';
fs.TimeSeparator:=':';
fs.ShortDateFormat:='yyyy-mm-dd';
fs.ShortTimeFormat:='hh:nn';
Result:=StrToDateTimeDef(DateStr,0,fs);
end;