只能用代码写啦: var s1,s2:string; s1:='20040410'; s2:=copy(s1,1,4)+'-'+copy(s1,5,2)+'-'+copy(s1,7,2) ; showmessage(s2);
copy的写法不太高明吧?用insert插入两个“-”再调用StrToDate转换!
procedure TForm1.Button7Click(Sender: TObject); var str:String; DateTime:TDateTime; begin str:='20040409'; DateTime:=StrToDateTime(FormatFloat('0000-00-00',StrToInt(str))); end;
gazo(不爱女兵我爱谁) ( ) 信誉:100 的代码我测试过!OK
uses MaskUtils; procedure TForm1.Button6Click(Sender: TObject); var S, EditMask: string; DateTime:TDateTime; begin S := '200402021456'; EditMask := '!99/99/00 99:99'; //datetime Caption:=MaskDoFormatText(EditMask, S, ' '); // S := '20040203'; EditMask := '!99/99/00'; //date Caption:=MaskDoFormatText(EditMask, S, #32); DateTime:=StrToDateTime(Caption); end;
这个方法显然不能一步完成转换function StrToDateTime(const S: string; const FormatSettings: TFormatSettings): TDateTime; overload;
这个 FormatSettings 该怎么设置才行呢?若是实在没有一步转换的方法的话, 自己写代码转换又该如何写最好? 谢谢大家了!
就是用MaskEdit,先设计它的文本格式。
使它能输入的内容只能为日期样式。然后再用strtodatetime()进行转换。
我没试过,没办法就试试这个吧。
var s1,s2:string;
s1:='20040410';
s2:=copy(s1,1,4)+'-'+copy(s1,5,2)+'-'+copy(s1,7,2) ;
showmessage(s2);
var
str:String;
DateTime:TDateTime;
begin
str:='20040409';
DateTime:=StrToDateTime(FormatFloat('0000-00-00',StrToInt(str)));
end;
的代码我测试过!OK
procedure TForm1.Button6Click(Sender: TObject);
var
S, EditMask: string;
DateTime:TDateTime;
begin
S := '200402021456';
EditMask := '!99/99/00 99:99'; //datetime
Caption:=MaskDoFormatText(EditMask, S, ' ');
//
S := '20040203';
EditMask := '!99/99/00'; //date
Caption:=MaskDoFormatText(EditMask, S, #32);
DateTime:=StrToDateTime(Caption);
end;