Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Obtener la fecha en cualquier idioma (https://www.clubdelphi.com/foros/showthread.php?t=80800)

cHackAll 24-05-2007 19:15:18

Obtener la fecha en cualquier idioma
 
Intentando obtener la forma de cambiar el idioma del Güindos produje una función no muy común que permite obtener la fecha en formato 'dddd, d "de" mmmm "del" yyyy' en cualquier idioma:

Código Delphi [-]
function GetLocaleStr(const Locale, LCType: Cardinal): ShortString;
begin
 Result[0] := Char(GetLocaleInfo(Locale, LCType, @Result[1], 255) - 1);
 Result[1] := Char(Byte(Result[1]) and not $20);
end;

function GetDateStr(DateTime: TDateTime; Lang: Cardinal): string;
var SystemTime: TSystemTime; Locale, LCID: Cardinal;
begin
 LCID := GetThreadLocale;
 SetThreadLocale(Lang);
 Locale := GetThreadLocale;
 DateTimeToSystemTime(DateTime, SystemTime);
 Result := GetLocaleStr(Locale, LOCALE_SDAYNAME1 + Byte(SystemTime.wDayOfWeek - 1) mod $F9) + ', ' + IntToStr(SystemTime.wDay) + ' de ' +
           GetLocaleStr(Locale, LOCALE_SMONTHNAME1 + SystemTime.wMonth - 1) + ' del ' + IntToStr(SystemTime.wYear);
 SetThreadLocale(LCID);
end;

Un ejemplo:
Código Delphi [-]
Caption := GetDateStr(Now, LANG_PORTUGUESE);

DarKraZY 28-05-2007 10:17:00

Hola!!

Creo que no sería necesario crear unas funciones tan "complejas", ya que hay funciones en Delphi que creo que realizan la misma acción. Por ejemplo, quedaría más sencillo de la siguiente manera

Código Delphi [-]
function GetDateStr(DateTime: TDateTime; Lang: Cardinal): string;
var
  fs: TFormatSettings;
begin
  GetLocaleFormatSettings(Lang, fs);
  Result := FormatDateTime('dddd, d "de" mmmm "del" yyyy', DateTime, fs);
end;

cHackAll 31-05-2007 00:12:16

Auchhhh! si es así dejo la funcion para las versiones que no cuentan con GetLocaleFormatSettings, si no para quienes desean conocer cómo funciona!


La franja horaria es GMT +2. Ahora son las 01:06:23.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi