so easy, set these Global varible first:
ShortDateFormat := 'yy"年"m"月"d"日"';
LongTimeFormat := 'h"时"nn"分"ss"秒"';
then
u can use DateTimeTostr, DateToStr to convert date to string.
DateTimeToStr use ShortDateFormat &
LongTimeFormat.
DateToStr use ShortDateFormat only.
see also:
DateSeperator
LongDateFormat
TimeAMString
TimePMString
ShortTimeFormat
....
if u need translate string to datetime first, then
convert it back to string, following function is what i wrote in my component:
function StrToDateDef(s: string): TDateTime;
const
monthdays : array [boolean, 1..12] of word = (
(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335),
(1, 32, 61, 92, 122, 153, 183, 214, 245, 275, 306, 336)
);
var
i, j, k: Integer;
dt : array [0..5] of integer;
begin
s := trim(s);
i := 1;
k := 0;
dt[0] := 0;
dt[1] := 1;
dt[2] := 1;
dt[3] := -1;
dt[4] := 0;
dt[5] := 0;
while (i <= length(s)) and not (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
j := i;
while j <= length(s)do
begin
while (i <= length(s)) and (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
dt[k] := strtoint(copy(s, j, i - j));
inc(k);
if k > 5 then
break;
while (i <= length(s)) and not (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
j := i;
end;
if dt[1] > 12 then
begin
dt[0] := dt[0] + (dt[1]-1) div 12 + 1;
dt[1] := (dt[1] - 1) div 12 + 1;
end;
if (dt[0] >=0) and (dt[0]<9999) then
begin
result := strtodate(inttostr(dt[0])+dateseparator+'1'+dateseparator+'1')-1;
i := monthdays[isleapyear(dt[0]), dt[1]];
result := result + i + dt[2] - 1;
if (dt[3] >= 0) then
begin
if dt[5] > 59 then
begin
dt[4] := dt[4] + dt[5] div 60;
dt[5] := dt[5] mod 60;
end;
if dt[4] > 59 then
begin
dt[3] := dt[3] + dt[4] div 60;
dt[4] := dt[4] mod 60;
end;
if dt[3] > 23 then
begin
result := result + dt[3] div 24;
dt[3] := dt[3] mod 24;
end;
if ((pos(TimePmString, s)<>0)
or (pos('PM', uppercase(s)) <> 0)) and (dt[3]<12) then
dt[3] := dt[3] + 12;
result := Result + EncodeTime(dt[3], dt[4], dt[5], 0);
end;
end else
result := now;
end;
this function even can translate the string "1999 25.333 56:61:01" to
correct datetime.