问题
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