宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取


unit
Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Winapi.msxml, System.DateUtils, Vcl.StdCtrls; type TForm1 = classTForm) Button1: TButton; Label1: TLabel; Label2: TLabel; procedure Button1ClickSender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //实时获取网络时间的函数, 得到的是格林威治时间; 默认从 sohu 服务器获取, 因为它最快, 平均只需 15 毫秒 function GetNetTimeaUrl: WideString = 'http://www.sohu.com'): string; begin with CoXMLHTTP.Create do begin open'Post', aUrl, False, EmptyParam, EmptyParam); sendEmptyParam); Result := getResponseHeader'Date'); end; end; //格林威治时间字符串)转换到北京时间 1 function GMT2BjDateTimeconst GMT: string): TDateTime; var A: TArray<string>; begin A := GMT.Split[',', ' '], ExcludeEmpty); //XE4 支持 with TStringList.Create do begin CommaText := 'Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12'; A[2] := Values[A[2]]; Free; end; Result := StrToDateTimeFormat'%s/%s/%s %s', [A[3], A[2], A[1], A[4]]), FormatSettings.Create2052)); Result := Result + 8/24; //换算成北京时间 end; //格林威治时间字符串)转换到北京时间 2 function GMT2BjDateTime2const GMT: string): TDateTime; var A: TArray<string>; begin A := GMT.Split[',', ' '], ExcludeEmpty); //XE4 支持 with TStringList.Create do begin CommaText := 'Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12'; A[2] := Values[A[2]]; Free; end; Result := StrToDateTimeFormat'%s/%s/%s %s', [A[3], A[2], A[1], A[4]]), FormatSettings.Create2052)); Result := Result + 8/24; //换算成北京时间 end; procedure TForm1.Button1ClickSender: TObject); var strGMT: string; bjDateTime: TDateTime; bjDateTime2: TDateTime; begin strGMT := GetNetTime); bjDateTime := GMT2BjDateTimestrGMT); bjDateTime2 := GMT2BjDateTime2strGMT); Label1.Caption:=strGMT + #10 + DateTimeToStrbjDateTime); Label2.Caption:=strGMT + #10 + DateTimeToStrbjDateTime2); end; end.

实时获取网络时间 并转换为北京时间的函数-风君子博客