How to convert UTC to local time

﹥>﹥吖頭↗ 提交于 2019-12-11 06:34:00

问题


I have to develop an application for ePrescribe and need to convert an UTC Time value (for example '2010-01-01T16:09:04.5Z') to local time. Delphi 2010, any suggestions?


回答1:


You have to parse the string manually first. Extract the individual values from it, then you can put them into a Win32 SYSTEMTIME record and call SystemTimeToTzSpecificLocalTime() to convert it from UTC to local. You can then use the converted SYSTEMTIME however you need, such as converting it to a TDateTime using SystemTimeToDateTime().




回答2:


Your computer provides your time zone. You can use them to manipulate UTC time to adjust by adding hours and minutes.




回答3:


You could use TXSDateTime class from unit XSBuiltIns

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  XSBuiltIns;

var xsDateTime: TXSDateTime;
    input, output:  string;
    date: TDateTime;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    input := '2010-01-01T16:09:04.5Z';
    xsDateTime := TXSDateTime.Create;
    xsDateTime.XSToNative(input);
    date := xsDateTime.AsDateTime;
    output := 'Parsed date/time: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', date);
    writeln(output);
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Output:

Parsed date/time: 2010-01-01 19:09:04


来源:https://stackoverflow.com/questions/15951273/how-to-convert-utc-to-local-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!