Reputation: 3713
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);
Please tell me how to convert the date format and display it?
Upvotes: 1
Views: 6302
Reputation: 595961
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]);
Upvotes: 1
Reputation: 5134
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
Upvotes: 4