DELPHI从网上获取北京时间


本文整理自网络,侵删。

 

引用单元 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);
  try
    XmlHttp.Send;
  except
    showmessage('网络未连通!');
  end;

  datetxt := XmlHttp.GetResponseHeader('Date');
  datetxt := Copy(datetxt, Pos(',', datetxt) + 1100);
  datetxt := StringReplace(datetxt, 'GMT''', []);
  datetxt := Trim(datetxt);
  if datetxt = '' then Exit;
  DateLst := TStringList.Create;
  while Pos(' ', datetxt) > 0 do
  begin
    DateLst.Add(Copy(datetxt, 1, Pos(' ', datetxt) - 1));
    datetxt := Copy(datetxt, Pos(' ', datetxt) + 1100);
  end;
  DateLst.Add(datetxt);


  if DateLst[1] = 'Jan' then
    mon := '01'
  else if DateLst[1] = 'Feb' then
    mon := '02'
  else if DateLst[1] = 'Mar' then
    mon := '03'
  else if DateLst[1] = 'Apr' then
    mon := '04'
  else if DateLst[1] = 'Mar' then
    mon := '05'
  else if DateLst[1] = 'Jun' then
    mon := '06'
  else if DateLst[1] = 'Jul' then
    mon := '07'
  else if DateLst[1] = 'Aug' then
    mon := '08'
  else if DateLst[1] = 'Sep' then
    mon := '09'
  else if DateLst[1] = 'Oct' then
    mon := '10'
  else if DateLst[1] = 'Nov' then
    mon := '11'
  else if DateLst[1] = 'Dec' then
mon := '12';


  s := DateLst[2] + '-' + mon + '-' + DateLst[0] + ' ' + DateLst[3];
//标准时间
//  ShowMessage(s);
// '/' or '-'
  timeGMT := StrToDateTime(DateLst[2] + '-' + mon + '-' + DateLst[0] + ' ' + DateLst[3]);
  //转换时区
  GetNetTime := IncHour(TimeGMT, 8);
//  ShowMessage(FormatDateTime('yyyy年mm月dd日 HH:NN:SS', GetNetTime));
  FreeAndNil(DateLst);
  Result:= FormatDateTime('yyyy-mm-dd HH:NN:SS', GetNetTime);
end;

相关阅读 >>

Delphi中combobox的datavalue值

Delphi之tclientsocket和tserversocket使用tcp keepalive心跳机制实现“断网”、"断电"检测

Delphi package

Delphi xe8 androdi利用httpclient实现的一个app自动更新组件

Delphi 2009 indy10 idhttp get 中文出现乱码的问题

Delphi 把一个ico转换为bmp

Delphi 带参数的 exit

Delphi删除目录及子目录及所有目录下的文件

Delphi编程之win10桌面图标设置

Delphi split 方法使用

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...