Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi UTC datetime string to TDateTime (https://www.delphipraxis.net/191522-utc-datetime-string-tdatetime.html)

WojTec 24. Jan 2017 11:10

Delphi-Version: 10 Berlin

UTC datetime string to TDateTime
 
There is sample datetime string in UTC:

Code:
UTC 2011-02-05 21:25:55
Is it routine to convert it to TDateTime? Or I must do it manually?

bra 24. Jan 2017 11:37

AW: UTC datetime string to TDateTime
 
You can use http://docwiki.embarcadero.com/Libra....ISO8601ToDate
There are a few other conversion functions in System.DateUtils that might be useful too.

Der schöne Günther 24. Jan 2017 11:40

AW: UTC datetime string to TDateTime
 
This is two steps:
  1. Parsing the String into a TDateTime
  2. Converting the TDateTime in UTC timezone to your local timezone

For Task 1, there is already a built-in routine if the String is a timestamp in ISO8601 format: ISO8601ToDate(..). This method can also automatically take care of converting to your local timezone or return UTC timezone.

But your example is not. A valid ISO8601 timestamp would look like
"2011-02-05T21:25:55Z"

If you're out of luck regarding ISO8601, I'd use regular expressions.

WojTec 24. Jan 2017 12:13

Re: UTC datetime string to TDateTime
 
Thank you guys! I did it classically (with lenght/pos/delete). Using RegEx will be better (for sure will be very simple, in two lines)?

a.def 24. Jan 2017 13:49

AW: UTC datetime string to TDateTime
 
Never in 2 lines. As already mentioned look here: How to convert to TDateTime an string formatted this way: 15h44m28s? [duplicate]

WojTec 24. Jan 2017 15:34

Re: UTC datetime string to TDateTime
 
Ok, thanks! Another question: how can I convert my datetime to ISO8601 with RegEx?

a.def 24. Jan 2017 15:42

AW: UTC datetime string to TDateTime
 
Decode your TDateTime.

DecodeDate - Extracts the year, month, day values from a TDateTime var.

Der schöne Günther 24. Jan 2017 15:49

AW: UTC datetime string to TDateTime
 
You don't need regular expressions if you just want to get the ISO8601 representation of a TDateTime. There's already a built-in DateToISO8601(..) method for that :-)


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:15 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz