如何将RFC 822日期格式转换为Delphi中的TDateTime类型?

更新于
2026-10-05 01:40:45
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计408个文字,预计阅读时间需要2分钟。

可以创建一个Delphi库函数来将RFC 822格式的日期字符串转换为TDateTime。以下是一个简单的函数示例:

delphifunction RFC822ToDateTime(const RFC822Date: string): TDateTime;var Year, Month, Day, Hour, Min, Sec, MSec: Word; IsAM: Boolean;begin Result :=0; if not ParseRFC822Date(RFC822Date, Year, Month, Day, Hour, Min, Sec, MSec, IsAM) then Exit;

Result :=EncodeDate(Year, Month, Day); Result :=Result + EncodeTime(Hour, Min, Sec, MSec);end;

function ParseRFC822Date(const RFC822Date: string; out Year, Month, Day, Hour, Min, Sec, MSec: Word; out IsAM: Boolean): Boolean;var S: string; i: Integer;begin Result :=False; S :=Trim(RFC822Date); if (Length(S) <20) or (S[19] ' ') then Exit;

// 解析日期部分 Year :=StrToIntDef(Copy(S, 1, 4), 0); Month :=StrToIntDef(Copy(S, 6, 2), 0); Day :=StrToIntDef(Copy(S, 9, 2), 0);

// 解析时间部分 Hour :=StrToIntDef(Copy(S, 12, 2), 0); Min :=StrToIntDef(Copy(S, 15, 2), 0); Sec :=StrToIntDef(Copy(S, 18, 2), 0); MSec :=0; // RFC 822格式不包含毫秒

// 解析AM/PM IsAM :=True; if Pos('AM', S) > 0 then IsAM :=True else if Pos('PM', S) > 0 then IsAM :=False;

// 校验日期和时间 if (Year > 0) and (Month in [1..12]) and (Day in [1..31]) and (Hour in [0..23]) and (Min in [0..59]) and (Sec in [0..59]) then Result :=True;end;

使用此函数,您可以这样转换日期字符串:

delphivar DateTimeValue: TDateTime;begin DateTimeValue :=RFC822ToDateTime('Mon, 24 Oct 2011 13:54:55 -0000'); // DateTimeValue 现在包含转换后的TDateTime值end;

任何人都可以建议一个将RFC 822格式的日期字符串转换为TDateTime的Delphi库吗?

例如2011年10月24日13:54:55 -0000

我想这可能是因为服务器返回的格式略有不同而容易出错,因此经过试验和测试的例程会很好.

使用自定义短日期格式sting,可以使用内置的StrToDateTime例程来实现吗?

注意:我正在使用Delphi 2010

Indy的 TIdDateTimeStamp(自Indy 9以来)有 SetFromRFC822方法,调用 StrInternetToDateTime.

本文共计408个文字,预计阅读时间需要2分钟。

可以创建一个Delphi库函数来将RFC 822格式的日期字符串转换为TDateTime。以下是一个简单的函数示例:

delphifunction RFC822ToDateTime(const RFC822Date: string): TDateTime;var Year, Month, Day, Hour, Min, Sec, MSec: Word; IsAM: Boolean;begin Result :=0; if not ParseRFC822Date(RFC822Date, Year, Month, Day, Hour, Min, Sec, MSec, IsAM) then Exit;

Result :=EncodeDate(Year, Month, Day); Result :=Result + EncodeTime(Hour, Min, Sec, MSec);end;

function ParseRFC822Date(const RFC822Date: string; out Year, Month, Day, Hour, Min, Sec, MSec: Word; out IsAM: Boolean): Boolean;var S: string; i: Integer;begin Result :=False; S :=Trim(RFC822Date); if (Length(S) <20) or (S[19] ' ') then Exit;

// 解析日期部分 Year :=StrToIntDef(Copy(S, 1, 4), 0); Month :=StrToIntDef(Copy(S, 6, 2), 0); Day :=StrToIntDef(Copy(S, 9, 2), 0);

// 解析时间部分 Hour :=StrToIntDef(Copy(S, 12, 2), 0); Min :=StrToIntDef(Copy(S, 15, 2), 0); Sec :=StrToIntDef(Copy(S, 18, 2), 0); MSec :=0; // RFC 822格式不包含毫秒

// 解析AM/PM IsAM :=True; if Pos('AM', S) > 0 then IsAM :=True else if Pos('PM', S) > 0 then IsAM :=False;

// 校验日期和时间 if (Year > 0) and (Month in [1..12]) and (Day in [1..31]) and (Hour in [0..23]) and (Min in [0..59]) and (Sec in [0..59]) then Result :=True;end;

使用此函数,您可以这样转换日期字符串:

delphivar DateTimeValue: TDateTime;begin DateTimeValue :=RFC822ToDateTime('Mon, 24 Oct 2011 13:54:55 -0000'); // DateTimeValue 现在包含转换后的TDateTime值end;

任何人都可以建议一个将RFC 822格式的日期字符串转换为TDateTime的Delphi库吗?

例如2011年10月24日13:54:55 -0000

我想这可能是因为服务器返回的格式略有不同而容易出错,因此经过试验和测试的例程会很好.

使用自定义短日期格式sting,可以使用内置的StrToDateTime例程来实现吗?

注意:我正在使用Delphi 2010

Indy的 TIdDateTimeStamp(自Indy 9以来)有 SetFromRFC822方法,调用 StrInternetToDateTime.