procedure TForm1.Button1Click(Sender: TObject); var str,s1,s2:string; i: integer; sl: Tstringlist; begin str:='08/08/12 11:00:00'; sl:=tstringlist.Create; sl.CommaText:=str; s1:=sl.Strings[0]; s2:=sl.Strings[1]; for i:=1 to length(s1) do begin if not (s1[i] in ['0'..'9']) then s1[i]:='-'; end; for i:=1 to length(s2) do begin if not (s2[i] in ['0'..'9']) then s2[i]:=':'; end; str:=s1+' '+s2; sl.Free; showmessage(str); end;
var DT : TDateTime; begin DT := VartoDateTime({字符串}) //~~~尽管会慢,但较安全 end;
uses StrUtils, DateUtils;function ToSystemDateTimeStr(s: String): String; var A : TFormatSettings; i, j : Integer; y, m, d, h, n, t: Word; begin i := Pos('-', s); y := StrToIntDef(copy(s, 1, i - 1), 0); j := PosEx('-', s, i + 1); m := StrToIntDef(copy(s, i + 1, j - i - 1), 0); i := PosEx(' ', s, j + 1); d := StrToIntDef(copy(s, j + 1, i - j - 1), 0); j := PosEx(':', s, i + 1); h := StrToIntDef(copy(s, i + 1, j - i - 1), 0); i := PosEx(':', s, j + 1); n := StrToIntDef(copy(s, j + 1, i - j - 1), 0); t := StrToIntDef(copy(s, i + 1, Length(S) - i), 0); GetLocaleFormatSettings(0, A); Result := FormatDateTime(A.ShortDateFormat + ' ' + A.LongTimeFormat, EncodeDateTime(y, m, d, h, n, t, 0)); end;procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage (ToSystemDateTimeStr('2008-8-26 18:12:34')); end;
var
s,str:string;
begin
s:=formatdatetime('yyyy"/"m"/"d" "h":"n":"s',now);
showmessage(s);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s,str:string;
a,b:pbyte;
adate:Tdatetime;
begin adate:=strtodatetime('2008-08-12 11:00:00');
s:=formatdatetime('yyyy"/"m"/"d" "h":"n":"s',adate);
showmessage(s);end;
象yyyy-MM-dd HH:mm:ss,yyyy/MM/dd HH:mm:ss,yy/MM/dd HH:mm:ss,yyyy/MM/dd tt h:mm:ss等等).
procedure TForm1.Button1Click(Sender: TObject);
var
str,s1,s2:string;
i: integer;
sl: Tstringlist;
begin
str:='08/08/12 11:00:00';
sl:=tstringlist.Create;
sl.CommaText:=str;
s1:=sl.Strings[0];
s2:=sl.Strings[1];
for i:=1 to length(s1) do
begin
if not (s1[i] in ['0'..'9']) then
s1[i]:='-';
end;
for i:=1 to length(s2) do
begin
if not (s2[i] in ['0'..'9']) then
s2[i]:=':';
end;
str:=s1+' '+s2;
sl.Free;
showmessage(str);
end;
DT : TDateTime;
begin
DT := VartoDateTime({字符串})
//~~~尽管会慢,但较安全
end;
uses StrUtils, DateUtils;function ToSystemDateTimeStr(s: String): String;
var
A : TFormatSettings;
i, j : Integer;
y, m, d, h, n, t: Word;
begin
i := Pos('-', s);
y := StrToIntDef(copy(s, 1, i - 1), 0);
j := PosEx('-', s, i + 1);
m := StrToIntDef(copy(s, i + 1, j - i - 1), 0);
i := PosEx(' ', s, j + 1);
d := StrToIntDef(copy(s, j + 1, i - j - 1), 0);
j := PosEx(':', s, i + 1);
h := StrToIntDef(copy(s, i + 1, j - i - 1), 0);
i := PosEx(':', s, j + 1);
n := StrToIntDef(copy(s, j + 1, i - j - 1), 0);
t := StrToIntDef(copy(s, i + 1, Length(S) - i), 0);
GetLocaleFormatSettings(0, A);
Result := FormatDateTime(A.ShortDateFormat + ' ' + A.LongTimeFormat, EncodeDateTime(y, m, d, h, n, t, 0));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage (ToSystemDateTimeStr('2008-8-26 18:12:34'));
end;
象yyyy-MM-dd HH:mm:ss,yyyy/MM/dd HH:mm:ss,yy/MM/dd HH:mm:ss,yyyy/MM/dd tt h:mm:ss等等).首先是所谓的系统时间格式是你系统的时间格式还是操作系统的时间格式。
1.操作系统的
GetFormatSettings
2.你系统的
找个地方存储你的掩码格式,调用的时候直接用就好了。
var
sysDatetimeFmt = 'yyyy-MM-dd HH:mm:ss'
通过接口动态更改他
调用的时候你转化就可以了。