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) + 1, 100);
  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) + 1, 100);
  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 抓屏技巧

Delphi 用stringhelper.split分解字符串

Delphi unigui程序部署到服务器

Delphi 判断字符串是否是单词

Delphi下用cdo发送邮箱实例(授权码cdo发送邮箱的解决办法)

Delphi之屏幕和窗口的坐标

shellexecute的多种用法

Delphi中的strpas功能

Delphi xe实现android 添加图片资源到应用并使用它

Delphi tms web core messagedlg对话框用法

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



打赏

取消

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

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

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

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

评论

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