![]() |
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 |
AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
Liste der Anhänge anzeigen (Anzahl: 1)
Das wird aus den Windows-Einstellungen übernommen:
|
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".
|
AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
Hallo,
Zitat:
Zitat:
|
AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
Offenbar hatte ich die Frage falsch verstanden.
Die Kommentare in SysUtils helfen da aber weiter: Zitat:
Zitat:
Zitat:
Zitat:
Zitat:
|
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:
Im Ergebnis kommt dann so etwas heraus:
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;
Code:
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.
12-31-2021-12-34-56
|
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:
Delphi-Quellcode:
var
FS: TFormatSettings; S: string; begin FS := TFormatSettings.Invariant; FS.DateSeparator := '-'; FS.TimeSeparator := '-'; S := DateTimeToStr(Now, FS); end; |
AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
Zitat:
Zitat:
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. |
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:
Das TFormatSettings.Invariant ist nur zur Konsistenz da und hat eigentlich keinen Einfluss.S := FormatDateTime('mm-dd-yyyy hh-mm-ss', Now, TFormatSettings.Invariant); |
AW: TFormatSettings Unterschied ShortDateFormat und LongDateFormat
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:59 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz