这个怎么使用? ( 积分: 100 )

  • 主题发起人 主题发起人 lqc11love
  • 开始时间 开始时间
L

lqc11love

Unregistered / Unconfirmed
GUEST, unregistred user!
http://www.delphibbs.com/keylife/iblog_show.asp?xid=11631中
根据时间日期格式从字符串中解析日期时间,下面这个怎么使用,举例说下????????????应该完成的功能是字符串转化时间???????
2004-10-23 13:42:15 再次修正,支持两位年表示function StrToDtFmt(const S, Fmt: String; Dft: TDateTime): TDateTime;
const
SShortMonthNameJan = 'Jan';
SShortMonthNameFeb = 'Feb';
SShortMonthNameMar = 'Mar';
SShortMonthNameApr = 'Apr';
SShortMonthNameMay = 'May';
SShortMonthNameJun = 'Jun';
SShortMonthNameJul = 'Jul';
SShortMonthNameAug = 'Aug';
SShortMonthNameSep = 'Sep';
SShortMonthNameOct = 'Oct';
SShortMonthNameNov = 'Nov';
SShortMonthNameDec = 'Dec';

SLongMonthNameJan = 'January';
SLongMonthNameFeb = 'February';
SLongMonthNameMar = 'March';
SLongMonthNameApr = 'April';
SLongMonthNameMay = 'May';
SLongMonthNameJun = 'June';
SLongMonthNameJul = 'July';
SLongMonthNameAug = 'August';
SLongMonthNameSep = 'September';
SLongMonthNameOct = 'October';
SLongMonthNameNov = 'November';
SLongMonthNameDec = 'December';

SMonthNames: array[1..12] of String = (
SShortMonthNameJan, // = 'Jan';
SShortMonthNameFeb, // = 'Feb';
SShortMonthNameMar, // = 'Mar';
SShortMonthNameApr, // = 'Apr';
SShortMonthNameMay, // = 'May';
SShortMonthNameJun, // = 'Jun';
SShortMonthNameJul, // = 'Jul';
SShortMonthNameAug, // = 'Aug';
SShortMonthNameSep, // = 'Sep';
SShortMonthNameOct, // = 'Oct';
SShortMonthNameNov, // = 'Nov';
SShortMonthNameDec // = 'Dec';
);
LMonthNames: array[1..12] of String = (
SLongMonthNameJan, // = 'January';
SLongMonthNameFeb, // = 'February';
SLongMonthNameMar, // = 'March';
SLongMonthNameApr, // = 'April';
SLongMonthNameMay, // = 'May';
SLongMonthNameJun, // = 'June';
SLongMonthNameJul, // = 'July';
SLongMonthNameAug, // = 'August';
SLongMonthNameSep, // = 'September';
SLongMonthNameOct, // = 'October';
SLongMonthNameNov, // = 'November';
SLongMonthNameDec // = 'December';
);
var
Pts: array[1..10] of Integer;
Wds: array[1..10] of Integer;
Vls: array[1..10] of Word;
i, j, n, m, k, d: Integer;
t: String;
c: Char;
dt: TDateTime;
function FindInArray(const S1: String; const SA: array of String): Integer;
var
i1: Integer;
begin
Result := -1;
for i1 := Low(SA) to High(SA) do
begin
if SameText(S1, SA[i1]) then
begin
Result := i1;
Exit;
end;
end;
end;
begin
// 只处理数字格式的日期和时间
i := 1;
n := 1;
t := Trim(AnsiUpperCase(Fmt));
// 解析格式串
while i <= Length(t) do
begin
case t of
'Y': Pts[n] := 1;
'M': Pts[n] := 2;
'D': Pts[n] := 3;
'H': Pts[n] := 4;
'N': Pts[n] := 5;
'S': Pts[n] := 6;
'Z': Pts[n] := 7;
'I': Pts[n] := 8;
else
begin
i := i + 1;
Continue;
end;
end;
c := t;
i := i + 1;
m := 1;
while t = c do
begin
Inc(i);
Inc(m);
end;
if t in ['Y','M','D','H','N','S','Z','I'] then
Wds[n] := m
else
Wds[n] := 0;
n := n + 1;
if n > 7 then Break;
end;
n := n - 1;
// 开始转化
Result := Dft;
if Length(S) <= 0 then Exit;
DecodeDate(Result, Vls[1], Vls[2], Vls[3]);
DecodeTime(Result, Vls[4], Vls[5], Vls[6], Vls[7]);
m := 1;
i := 1;
k := Length(S);
while m <= n do
begin
if (Pts[m] = 2) or (Pts[m] = 8) then
begin
while not (S in ['0'..'9', 'a'..'z', 'A'..'Z', #0]) do Inc(i);
end
else
begin
while not (S in ['0'..'9', #0]) do Inc(i);
end;
if i > k then Break;
d := 0;
j := i;

if (Pts[m] = 2) or (Pts[m] = 8) then
begin
if S in ['0'..'9'] then
begin
while (S in ['0'..'9']) and
((Wds[m] <= 0) or (i - j < Wds[m])) do
begin
d := d * 10 + Ord(S) - Ord('0');
i := i + 1;
end;
end
else
begin
while (S in ['0'..'9', 'a'..'z', 'A'..'Z']) and
((Wds[m] <= 0) or (i - j < Wds[m])) do
begin
i := i + 1;
end;
d := i - j;
t := Copy(S, j, d);
d := StrToIntDef(t, -1);
if d < 0 then
d := FindInArray(t, SMonthNames);
if d < 0 then
d := FindInArray(t, LMonthNames);
if d < 0 then
d := FindInArray(t, ShortMonthNames);
if d < 0 then
d := FindInArray(t, LongMonthNames);
if d >= 0 then d := d mod 12 + 1;
end;
if (d >= 0) and (d < 50) and (Pts[m] = 1) then
d := d + 2000;
if d >= 1 then Vls[Pts[m]] := d;
end
else
begin
begin
while (S in ['0'..'9']) and
((Wds[m] <= 0) or (i - j < Wds[m])) do
begin
d := d * 10 + Ord(S) - Ord('0');
i := i + 1;
end;
end;
if (Pts[m] <= 3) and (d >= 0) then
Vls[Pts[m]] := d
else
Vls[Pts[m]] := d;
end;
if i > k then Break;
m := m + 1;
end;
if TryEncodeDate(Vls[1], Vls[2], Vls[3], dt) then
Result := Int(dt) + Frac(Result);
if TryEncodeTime(Vls[4], Vls[5], Vls[6], Vls[7], dt) then
Result := Int(Result) + Frac(dt);
end;
 
简单问题没人回答,分只有给自己了
 
接受答案了.
 

Similar threads

S
回复
0
查看
700
SUNSTONE的Delphi笔记
S
S
回复
0
查看
694
SUNSTONE的Delphi笔记
S
I
回复
0
查看
639
import
I
后退
顶部