delphidelphi-7regional-settings

delphi dateformat MM|dd|yy to MM/dd/yy error


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?


Solution

  • 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