delphi 获取北京时间(使用XMLHTTP获取百度的时间,WebBrowser获取www.timedate.cn的时间)

时间:2022-07-13 14:39:27

方法一:

uses
ComObj, DateUtils; function GetInternetTime: string;
var
XmlHttp: OleVariant;
datetxt: string;
DateLst: TStringList;
mon: string;
timeGMT, GetNetTime: TDateTime;
s: string;
begin
XmlHttp := CreateOleObject('Microsoft.XMLHTTP');
XmlHttp.Open('GET', 'http://open.baidu.com/special/time/', False);
XmlHttp.Send; datetxt := XmlHttp.GetResponseHeader('Date');
datetxt := Copy(datetxt, Pos(',', datetxt) + , );
datetxt := StringReplace(datetxt, 'GMT', '', []);
datetxt := Trim(datetxt);
if datetxt = '' then Exit;
DateLst := TStringList.Create;
while Pos(' ', datetxt) > do
begin
DateLst.Add(Copy(datetxt, , Pos(' ', datetxt) - ));
datetxt := Copy(datetxt, Pos(' ', datetxt) + , );
end;
DateLst.Add(datetxt);
if DateLst[] = 'Jan' then
mon := ''
else if DateLst[] = 'Feb' then
mon := ''
else if DateLst[] = 'Mar' then
mon := ''
else if DateLst[] = 'Apr' then
mon := ''
else if DateLst[] = 'Mar' then
mon := ''
else if DateLst[] = 'Jun' then
mon := ''
else if DateLst[] = 'Jul' then
mon := ''
else if DateLst[] = 'Aug' then
mon := ''
else if DateLst[] = 'Sep' then
mon := ''
else if DateLst[] = 'Oct' then
mon := ''
else if DateLst[] = 'Nov' then
mon := ''
else if DateLst[] = 'Dec' then
mon := '';
s := DateLst[] + '-' + mon + '-' + DateLst[] + ' ' + DateLst[];
//标准时间
// ShowMessage(s);
// '/' or '-'
timeGMT := StrToDateTime(DateLst[] + '-' + mon + '-' + DateLst[] + ' ' + DateLst[]);
//转换时区
GetNetTime := IncHour(TimeGMT, );
// ShowMessage(FormatDateTime('yyyy年mm月dd日 HH:NN:SS', GetNetTime));
FreeAndNil(DateLst);
Result:= FormatDateTime('yyyy年mm月dd日 HH:NN:SS', GetNetTime);
end;

方法二:

procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser.Navigate('http://www.timedate.cn/worldclock/ti.asp');
tmr1.Enabled := True;
end; procedure TForm1.WebBrowserStatusTextChange(Sender: TObject;
const Text: WideString);
var
strText: string;
begin
strText := Text;
if strText = '完成' Then
begin
Caption := WebBrowser.OleObject.Document.Body.Innertext;
tmr1.Enabled := True
end;
end; procedure TForm1.tmr1Timer(Sender: TObject);
begin
tmr1.Enabled := False;
WebBrowser.Refresh
end;

http://blog.csdn.net/yanjiaye520/article/details/7887463