Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Wie bekomme ich eine US-Datumszeit (https://www.delphipraxis.net/206732-wie-bekomme-ich-eine-us-datumszeit.html)

Alallart 23. Jan 2021 16:32

Wie bekomme ich eine US-Datumszeit
 
Wobei mich sowohl die Eingabe, wie auch die Ausgabe interessiert.

Den Typ TFormatSettings kenne ich, habe schon gelegentlich mit ihm gearbeitet. Allerdings drehte sich das stets um das Datum. Soweit kein Problem. Nun bekomme ich Datum und Zeit auf amerikanisch, also z. B. 01/23/2021 05:28 PM. Und um diese AM und PM geht es mir. Soweit ist das unkompliziert, man verändert z. B. bei ShortDateFormat den Formatstring. Wie aber gibt man TFormatSettings zu verstehen, dass die Uhrzeit amerikanisch ist?

Alallart 23. Jan 2021 18:29

AW: Wie bekomme ich eine US-Datumszeit
 
Ich habe etwas experimentiert, und habe es hinbekommen. Vielleicht braucht es jemand später auch, deshalb hier der Code:
Delphi-Quellcode:
function US_FormatDateTimeSettings: TFormatSettings;
begin
  //GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, Result);
  Result.DateSeparator := '/';
  Result.TimeSeparator := ':';
  Result.ShortDateFormat := 'MM/dd/yyyy';
  Result.ShortTimeFormat := 'hh:mm am/pm';
  Result.LongTimeFormat   := 'hh:mm:ss am/pm';
end;

function Default_FormatDateTimeSettings: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, Result);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyDate: TDateTime;
begin
  MyDate := StrToDateTime(Edit1.Text, US_FormatDateTimeSettings);

  ShowMessage(DateTimeToStr(MyDate, Default_FormatDateTimeSettings));
end;
Gibt man in Edit1 das Datum und Zeit im Format 01/23/2021 07:27 pm, kommt bei ShowMessage das Format 23.01.2021 19:27:00 raus.

TiGü 23. Jan 2021 21:17

AW: Wie bekomme ich eine US-Datumszeit
 
Schau dir mal an, was TFormatSettings.Create dir anbietet.
Mit eine Aufruf von
Delphi-Quellcode:
TFormatSettings.Create('en-US')
hast du ein fertig und komplett ausfülltes FormatSettings für den angloamerikanischen Sprachraum.

Alallart 23. Jan 2021 21:35

AW: Wie bekomme ich eine US-Datumszeit
 
Ich dachte immer TFormatSettings ist nur ein Typ? Ich habe eine etwas ältere Version von Delphi. So wie es aussieht geht das nicht bei mir.

Programmer87 24. Jan 2021 07:47

AW: Wie bekomme ich eine US-Datumszeit
 
Zitat:

Zitat von Alallart (Beitrag 1481454)
Ich habe etwas experimentiert, und habe es hinbekommen. Vielleicht braucht es jemand später auch, deshalb hier der Code:
Delphi-Quellcode:
function US_FormatDateTimeSettings: TFormatSettings;
begin
  //GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, Result);
  Result.DateSeparator := '/';
  Result.TimeSeparator := ':';
  Result.ShortDateFormat := 'MM/dd/yyyy';
  Result.ShortTimeFormat := 'hh:mm am/pm';
  Result.LongTimeFormat   := 'hh:mm:ss am/pm';
end;

function Default_FormatDateTimeSettings: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, Result);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  MyDate: TDateTime;
begin
  MyDate := StrToDateTime(Edit1.Text, US_FormatDateTimeSettings);

  ShowMessage(DateTimeToStr(MyDate, Default_FormatDateTimeSettings));
end;
Gibt man in Edit1 das Datum und Zeit im Format 01/23/2021 07:27 pm, kommt bei ShowMessage das Format 23.01.2021 19:27:00 raus.

Super Danke!! :) Das ist wirklich sehr hilfreich! Ich hatte das gleich Problem :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:39 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