Pregunta

I need to convert my current date which has the format:

     MM|dd|yy     ---  12|09|11

I need to convert the format to:

     MM/dd/yy     --12/09/11

The current system date separator is:

   -'|'

I use the code as:

  var
     sDateOne : TDate ;
 begin
   Label1.Caption:=datetostr(now);              {this display as 12|09|11}
   ShortDateFormat:='MM/dd/yy';
   DateSeparator:='/';
   sDateOne:=StrToDate(Label1.Caption);
   FormatDateTime('MM/dd/yy',sDateOne );
   Label2.Caption:=datetostr(sDateOne);         {this i want as 12/09/11 }
 end;

but I get an error at line sDateOne:=StrToDate(Label1.Caption); enter image description here

Please tell me how to convert the date format and display it?

¿Fue útil?

Solución

Here is corrected version of your code:

var
  DateOne: TDate;
  LocalFormatSettings: TFormatSettings;
begin
  Label1.Caption := datetostr(now);              {this display as 12|09|11}
  DateOne := StrToDate(Label1.Caption);
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, LocalFormatSettings);
  LocalFormatSettings.DateSeparator := '/';
  Label2.Caption := FormatDateTime('MM/dd/yy', DateOne, LocalFormatSettings); {this i want as 12/09/11 }
end;

For GetLocaleFormatSettings information, please see http://delphi.about.com/library/rtl/blrtlGetLocaleFormatSettings.htm

Otros consejos

If all you are doing is changing the separator, and not the order of the numbers, then you could simply use StringReplace(), eg:

var
  S: String;

S := '12|09|11';
S := StringReplace(S, '|', '/', [rfReplaceAll]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top