StrToDateTime这个函数只能把符合“locale’s date/time format”的字符串变成datetime,现在我有一个字符串,不符合这个格式,但是符合其他时间格式,怎么变成时间?比如说现在我可以把5/19/2003 7:57:26 PM通过strtodatetime变成tdatetime类型,但是我有一个Jun 3 2003 03:04这种格式的字符串。怎么变成tdatetime类型?有没有别的函数。
解决方案 »
- Loadfromfile能否把图片load到不同的位置?
- 如何合并两个程序,让一个程序运行完毕后再运行另一个程序!
- 如何动态生成lable这个控件?
- 先解决问题者50分。现有一TPageControl(两个TabSheet),上面没有任何控件,希望在程序运行时把Form1,和Form2中的控件分别显示在TPageCotr
- 关于delphi的ide的一个小bug?
- 高分求购:谁有NOKIA8250的短信程序源码?
- 如何用修改注册表的方法修改系统的日期格式
- infopower 3000的问题
- 怎样用程序得到共享文件的共享名?在线等待!
- 如何取得IE的临时目录
- 自动拨号的问题?
- Xleep(笑尽天下事) 进来看看~~~
*等我有钱了,我就站在天安门广场挨个给大家发钱*
**********************************************
短消息已收到,我用的是D7,代码在SysUtils.Pas 中,相关函数如下
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
var
Pos: Integer;
Date, Time: TDateTime;
begin
Result := True;
Pos := 1;
Time := 0;
if not ScanDate(S, Pos, Date) or
not ((Pos > Length(S)) or ScanTime(S, Pos, Time)) then // Try time only
Result := TryStrToTime(S, Value)
else
if Date >= 0 then
Value := Date + Time
else
Value := Date - Time;
end;function ScanDate(const S: string; var Pos: Integer; var Date: TDateTime;
const FormatSettings: TFormatSettings): Boolean; overload;
var
DateOrder: TDateOrder;
N1, N2, N3, Y, M, D: Word;
L1, L2, L3, YearLen: Byte;
CenturyBase: Integer;
EraName : string;
EraYearOffset: Integer; function EraToYear(Year: Integer): Integer;
begin
{$IFDEF MSWINDOWS}
if SysLocale.PriLangID = LANG_KOREAN then
begin
if Year <= 99 then
Inc(Year, (CurrentYear + Abs(EraYearOffset)) div 100 * 100);
if EraYearOffset > 0 then
EraYearOffset := -EraYearOffset;
end
else
Dec(EraYearOffset);
{$ENDIF}
Result := Year + EraYearOffset;
end;begin
Y := 0;
M := 0;
D := 0;
YearLen := 0;
Result := False;
DateOrder := GetDateOrder(FormatSettings.ShortDateFormat);
EraYearOffset := 0;
if FormatSettings.ShortDateFormat[1] = 'g' then // skip over prefix text
begin
ScanToNumber(S, Pos);
EraName := Trim(Copy(S, 1, Pos-1));
EraYearOffset := GetEraYearOffset(EraName);
end
else
if AnsiPos('e', FormatSettings.ShortDateFormat) > 0 then
EraYearOffset := EraYearOffsets[1];
if not (ScanNumber(S, Pos, N1, L1) and ScanChar(S, Pos, FormatSettings.DateSeparator) and
ScanNumber(S, Pos, N2, L2)) then Exit;
if ScanChar(S, Pos, FormatSettings.DateSeparator) then
begin
if not ScanNumber(S, Pos, N3, L3) then Exit;
case DateOrder of
doMDY: begin Y := N3; YearLen := L3; M := N1; D := N2; end;
doDMY: begin Y := N3; YearLen := L3; M := N2; D := N1; end;
doYMD: begin Y := N1; YearLen := L1; M := N2; D := N3; end;
end;
if EraYearOffset > 0 then
Y := EraToYear(Y)
else
if (YearLen <= 2) then
begin
CenturyBase := CurrentYear - FormatSettings.TwoDigitYearCenturyWindow;
Inc(Y, CenturyBase div 100 * 100);
if (FormatSettings.TwoDigitYearCenturyWindow > 0) and (Y < CenturyBase) then
Inc(Y, 100);
end;
end else
begin
Y := CurrentYear;
if DateOrder = doDMY then
begin
D := N1; M := N2;
end else
begin
M := N1; D := N2;
end;
end;
ScanChar(S, Pos, FormatSettings.DateSeparator);
ScanBlanks(S, Pos);
if SysLocale.FarEast and (System.Pos('ddd', FormatSettings.ShortDateFormat) <> 0) then
begin // ignore trailing text
if FormatSettings.ShortTimeFormat[1] in ['0'..'9'] then // stop at time digit
ScanToNumber(S, Pos)
else // stop at time prefix
repeat
while (Pos <= Length(S)) and (S[Pos] <> ' ') do Inc(Pos);
ScanBlanks(S, Pos);
until (Pos > Length(S)) or
(AnsiCompareText(FormatSettings.TimeAMString,
Copy(S, Pos, Length(FormatSettings.TimeAMString))) = 0) or
(AnsiCompareText(FormatSettings.TimePMString,
Copy(S, Pos, Length(FormatSettings.TimePMString))) = 0);
end;
Result := TryEncodeDate(Y, M, D, Date);
end;function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
var
Pos: Integer;
begin
Pos := 1;
Result := ScanTime(S, Pos, Value) and (Pos > Length(S));
end;
function ScanTime(const S: string; var Pos: Integer;
var Time: TDateTime): Boolean; overload;
var
BaseHour: Integer;
Hour, Min, Sec, MSec: Word;
Junk: Byte;
begin
Result := False;
BaseHour := -1;
if ScanString(S, Pos, TimeAMString) or ScanString(S, Pos, 'AM') then
BaseHour := 0
else if ScanString(S, Pos, TimePMString) or ScanString(S, Pos, 'PM') then
BaseHour := 12;
if BaseHour >= 0 then ScanBlanks(S, Pos);
if not ScanNumber(S, Pos, Hour, Junk) then Exit;
Min := 0;
Sec := 0;
MSec := 0;
if ScanChar(S, Pos, TimeSeparator) then
begin
if not ScanNumber(S, Pos, Min, Junk) then Exit;
if ScanChar(S, Pos, TimeSeparator) then
begin
if not ScanNumber(S, Pos, Sec, Junk) then Exit;
if ScanChar(S, Pos, DecimalSeparator) then
if not ScanNumber(S, Pos, MSec, Junk) then Exit;
end;
end;
if BaseHour < 0 then
if ScanString(S, Pos, TimeAMString) or ScanString(S, Pos, 'AM') then
BaseHour := 0
else
if ScanString(S, Pos, TimePMString) or ScanString(S, Pos, 'PM') then
BaseHour := 12;
if BaseHour >= 0 then
begin
if (Hour = 0) or (Hour > 12) then Exit;
if Hour = 12 then Hour := 0;
Inc(Hour, BaseHour);
end;
ScanBlanks(S, Pos);
Result := TryEncodeTime(Hour, Min, Sec, MSec, Time);
end;