Convert UTC string to TDatetime in Delphi

var tm : string; dt : tdatetime; tm := '2009-08-21T09:11:21Z'; dt := ? 

I know that I can parse it manually, but I wonder if there is any built-in function or Win32 API function for this?

+4
source share
3 answers

I don’t know why so many people shoot in their mouth when they don’t know what they are talking about? I have to do this dirty job; Is this a RAD tool? Sometimes I find Delphi to have a great architecture.

 procedure setISOtoDateTime(strDT: string); var // Delphi settings save vars ShortDF, ShortTF : string; TS, DS : char; // conversion vars dd, tt, ddtt: TDateTime; begin // example datetime test string in ISO format strDT := '2009-07-06T01:53:23Z'; // save Delphi settings DS := DateSeparator; TS := TimeSeparator; ShortDF := ShortDateFormat; ShortTF := ShortTimeFormat; // set Delphi settings for string to date/time DateSeparator := '-'; ShortDateFormat := 'yyyy-mm-dd'; TimeSeparator := ':'; ShortTimeFormat := 'hh:mm:ss'; // convert test string to datetime try dd := StrToDate( Copy(strDT, 1, Pos('T',strDT)-1) ); tt := StrToTime( Copy(strDT, Pos('T',strDT)+1, 8) ); ddtt := trunc(dd) + frac(tt); except on EConvertError do ShowMessage('Error in converting : ' + strDT); end; // restore Delphi settings DateSeparator := DS; ShortDateFormat := ShortDF; TimeSeparator := TS; ShortTimeFormat := ShortTF; // display test string ShowMessage ( FormatDateTime('mm/dd/yyyy hh:mm:ss', ddtt) ); end; 

http://coding.derkeiler.com/Archive/Delphi/comp.lang.pascal.delphi.misc/2006-08/msg00190.html

+3
source

This is similar to the activity associated with the Internet Protocol, so you should not have problems using the Win32 API for this. However, note that Windows does not correctly support converting to / from UTC for historical dates that are more than 20 years old - Windows simply does not have enough details in the settings of its time zone.

0
source

If you are using Indy 10, its functions StrInternetToDateTime() and GMTToLocalDateTime() (in the IdGlobalProtocols module) can handle ISO-8601 formatted strings.

0
source

Source: https://habr.com/ru/post/915319/


All Articles