Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi TFormatSettings Unterschied ShortDateFormat und LongDateFormat (https://www.delphipraxis.net/208266-tformatsettings-unterschied-shortdateformat-und-longdateformat.html)

Codehunter 6. Jul 2021 09:28

Delphi-Version: 10.4 Sydney

TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Hallo!

Immer wenn ich mit DateTimeToStr oder FormatDateTime zu tun habe, stelle ich mir wieder die selbe Frage: Wann und für was werden ShortDateFormat und LongDateFormat in TFormatSettings verwendet? Meistens weise ich beiden den selben Formatstring zu, um auf Nummer Sicher zu gehen. Aber das ist ja sicher nicht Sinn der Sache.

Grüße
Cody

Uwe Raabe 6. Jul 2021 09:45

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Liste der Anhänge anzeigen (Anzahl: 1)
Das wird aus den Windows-Einstellungen übernommen:

Codehunter 6. Jul 2021 10:00

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
@Uwe: Sorry ich steh immer noch auf dem Schlauch. Mir ist weder bei den genannten Delphi-Funktionen noch bei Windows ein Schalter bekannt, der besagt "nimm das lange Datum" oder "nimm das kurze Datum".

hoika 6. Jul 2021 10:11

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Hallo,
Zitat:

auf dem Schlauch
DateToStr nimmt z.B. entweder das kurze oder das lange Datumsformat (ich glaube das kurze).

Zitat:

der besagt "nimm das lange Datum" oder "nimm das kurze Datum"
Und es gibt ja jetzt überladene Funktionen, wo die gewünschten FormatSettings mit übergeben werden können.

Uwe Raabe 6. Jul 2021 10:26

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Offenbar hatte ich die Frage falsch verstanden.

Die Kommentare in SysUtils helfen da aber weiter:
Zitat:

{ DateToStr converts the date part of the given TDateTime value to a string.
The conversion uses the format specified by the ShortDateFormat global
variable. }
Zitat:

{ TimeToStr converts the time part of the given TDateTime value to a string.
The conversion uses the format specified by the LongTimeFormat global
variable. }
Zitat:

{ DateTimeToStr converts the given date and time to a string. The resulting
string consists of a date and time formatted using the ShortDateFormat and
LongTimeFormat global variables. Time information is included in the
resulting string only if the fractional part of the given date and time
value is non-zero. }
Zitat:

{ StrToDate converts the given string to a date value. The string must
consist of two or three numbers, separated by the character defined by
the DateSeparator global variable. The order for month, day, and year is
determined by the ShortDateFormat global variable--possible combinations
are m/d/y, d/m/y, and y/m/d. If the string contains only two numbers, it
is interpreted as a date (m/d or d/m) in the current year. Year values
between 0 and 99 are assumed to be in the current century. If the given
string does not contain a valid date, an EConvertError exception is
raised. }
Zitat:

{ FormatDateTime formats the date-and-time value given by DateTime using the
format given by Format. The following format specifiers are supported:

c Displays the date using the format given by the ShortDateFormat
global variable, followed by the time using the format given by
the LongTimeFormat global variable. The time is not displayed if
the fractional part of the DateTime value is zero.
...
ddddd Displays the date using the format given by the ShortDateFormat
global variable.

dddddd Displays the date using the format given by the LongDateFormat
global variable.

Codehunter 6. Jul 2021 14:25

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
@Uwe: Danke, das hilft tatsächlich ein bisschen weiter. Aber besonders bei DateTimeToStr ist mir das nach wie vor unklar.

Vielleicht sollte ich noch ergänzen, dass ich TFormatSettings selten mit .Create erzeuge. Das heißt, die Systemeinstellungen sollen mit DateTimeToStr(myDateTimeVar, myFormatSettings) ganz bewusst übersteuert werden. Das hat die Bewandnis, dass ich mich viel mit externen Programmen unterhalten muss, die hartcodierte Zeitformate verwenden.

Ein typischer Initialisierungsblock sieht bei mir so aus:
Delphi-Quellcode:
var
  FS: TFormatSettings;
  S: string;
begin
  FS.ShortDateFormat := 'mm/dd/yyyy';
  FS.LongDateFormat := FS.ShortDateFormat;
  FS.ShortTimeFormat := 'hh:nn:ss';
  FS.LongTimeFormat := FS.ShortTimeFormat;
  FS.DateSeparator := '-';
  FS.TimeSeparator := '-';
  S := DateTimeToStr(Now, FS).Replace(' ', '-');
end;
Im Ergebnis kommt dann so etwas heraus:
Code:
12-31-2021-12-34-56
Dieses LongxxxFormat := ShortxxxFormat ist genau das, was ich nur aus Unsicherheit heraus mache, weil ich nicht weiß unter welchen Voraussetzungen DateTimeToStr das eine oder das andere nimmt.

Uwe Raabe 6. Jul 2021 14:37

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Es ist ja auch etwas verwirrend wenn DateTimeToStr für das Datum das ShortDateFormat aber für die Zeit das LongTimeFormat nimmt.

Zitat:

Zitat von Codehunter (Beitrag 1491951)
Delphi-Quellcode:
var
  FS: TFormatSettings;
  S: string;
begin
  FS.ShortDateFormat := 'mm/dd/yyyy';
  FS.LongDateFormat := FS.ShortDateFormat;
  FS.ShortTimeFormat := 'hh:nn:ss';
  FS.LongTimeFormat := FS.ShortTimeFormat;
  FS.DateSeparator := '-';
  FS.TimeSeparator := '-';
  S := DateTimeToStr(Now, FS).Replace(' ', '-');
end;
Im Ergebnis kommt dann so etwas heraus:
Code:
12-31-2021-12-34-56

Hast du mal probiert, ob das nicht auch so funktioniert?
Delphi-Quellcode:
var
  FS: TFormatSettings;
  S: string;
begin
  FS := TFormatSettings.Invariant;
  FS.DateSeparator := '-';
  FS.TimeSeparator := '-';
  S := DateTimeToStr(Now, FS);
end;

Codehunter 6. Jul 2021 16:21

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1491952)
Es ist ja auch etwas verwirrend wenn DateTimeToStr für das Datum das ShortDateFormat aber für die Zeit das LongTimeFormat nimmt.

Ja und nicht nur das. Sondern auch die Ungewissheit, in wie fern da noch Regional- und Systemeinstellungen mit rein spielen. Das kann man ja auch nur begrenzt testen.

Zitat:

Zitat von Uwe Raabe (Beitrag 1491952)
Hast du mal probiert, ob das nicht auch so funktioniert?
Delphi-Quellcode:
var
  FS: TFormatSettings;
  S: string;
begin
  FS := TFormatSettings.Invariant;
  FS.DateSeparator := '-';
  FS.TimeSeparator := '-';
  S := DateTimeToStr(Now, FS);
end;

Im vorgenannten Beispiel könnte das - mit Ausnahme des Bindestriches zwischen Datum und Uhrzeit - funktionieren. Aber ich habe zahlreiche ganz unterschiedliche Formatierungen, die nicht so eng an das englische Format angelehnt sind. Dann ginge das wieder nicht mehr.

Also wenn ich das so rekapituliere scheint es nicht völlig übertrieben zu sein, Shortxxx und Longxxx jeweils gleich zu belegen. Damit einher geht dann für mich die Frage, wie sinnhaltig die threadsichere zweite Implementierung von FormatDateTime ist, die dann ja mit fünf (!!!) evtl. unterschiedlichen Formatstrings hantiert.

Uwe Raabe 6. Jul 2021 16:40

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Die Threadsicherheit wird dadurch hergestellt, dass die FormatSettings entweder nur in einem Thread verwendet werden oder einmal eingestellt nicht mehr verändert werden. Ich frage mich aber, warum du überhaupt die vordefinierten FormatStrings nimmst, wenn du solche Spezialitäten brauchst. Dann kannst du den FormatString doch bei FormatDateTime direkt übergeben.
Delphi-Quellcode:

  S := FormatDateTime('mm-dd-yyyy hh-mm-ss', Now, TFormatSettings.Invariant);
Das TFormatSettings.Invariant ist nur zur Konsistenz da und hat eigentlich keinen Einfluss.

Codehunter 6. Jul 2021 16:49

AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
 
Zitat:

Zitat von Uwe Raabe (Beitrag 1491962)
Delphi-Quellcode:
  S := FormatDateTime('mm-dd-yyyy hh-mm-ss', Now, TFormatSettings.Invariant);

Stop stop stop! Das musst du mir jetzt mal erklären. Ich dachte immer, solche Formatstrings wären ungültig? Wegen der Date- bzw. Time-Separatoren, die dann entweder mit "/" (Date) oder ":" (Time) als Platzhalter angegeben werden müssen und dann erst vom Formatierer ersetzt werden? Deswegen mach ich ja den ganzen Aufriss mit den Formatsettings. Wie gesagt, immer im Hinterkopf, dass mir da u.U. das System und die Regionalsettings reinpfuschen könnten.


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:04 Uhr.
Seite 1 von 2  1 2      

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