3
votes

Data I am having

I have a data stored in database which has a date and time stored as a string.

This data is stored at the central server. In this data the date and time has been stored in different formats (varies as per the client machines date time format).

I wanted to do

Now, I want to convert these all different formatted date and time strings to "yyyy-mm-dd hh:mm:ss" format.

Problem I am facing

I am not able to convert date-time data with multiple date time format to a single format.

Example: My machine date time format is yyyy-mm-dd. but when I am trying to convert the date below

StrToDateTime('2015/02/10')

It throws an exception.

Can any one tell me how I can achieve that? Please let me know if any more details are required. Thank you in advance.

3
Unfortunately, this is the end-result of poor coding. A date of 02/10/15 might be February 10th 2015, October 2nd 2015, or October 15th 2002. The solution is to not store it in this format in the first place.Jerry Dodge
Replace the '/' characters with '-'.LU RD
On which machine are you going to perform the conversion? The same machine that wrote the data? The big lesson to learn from this is that formatting of dates, numbers etc. should be treated as an issue of display and/or input. Store the data internally in a well defined format. Only at the program boundary, the interface with the user should you use locale formatting.David Heffernan
@DavidHeffernan No, I am doing conversion on different machine.The writing of data is done from other machines which has different date and time formats. Thanks david for your suggestion I will keep it in mind. you are always there to help me on my questions. Thanks.A B
Why would you ever store a date/time value as a string in a database? Most databases have proper date/time data types that should be used instead. They do not suffer from this problem.Remy Lebeau

3 Answers

8
votes

You need custom date format settings, like that:

procedure Test;
var
  DT: TDateTime;
  FS: TFormatSettings;

begin
  FS:= TFormatSettings.Create;
  FS.DateSeparator:= '/';
  FS.ShortDateFormat:= 'yy/m/d';
  DT:= StrToDateTime('2015/02/10', FS);
  Writeln(FormatDateTime('dd mm yyyy', DT));
end;
1
votes

In this procedure is error , because cannot be used FS:= TFormatSettings.Create; TformatSettings is reserved only for System . TformatSettings not have Create method . On this line compiler tell that Craeate is not know Sou I recomended this procedure to convert from one string to another string in a different format

enter code here

Procedure TForm4.ConvertDate(var DataValue:string);   
var
        AFormatSettings:TFormatSettings;  
        ALocaleID: integer;  
        DateTym :TdateTime;

begin  
 DataValue:=StringReplace(DataValue, '/',{OldPattern} '-', {NewPattern}     
                 [rfReplaceAll, rfIgnoreCase] {Flags:TreplaceFlags});     
  //this StringReplace must be used because with Separator  / for date    
   //occurs error          
 GetLocaleFormatSettings(ALocaleID, AFormatSettings);  
 AFormatSettings.ShortDateFormat:='DD-MM-YYYY';   
 DateTym:=StrToDate(DataValue, AFormatSettings);     
 AFormatSettings.ShortDateFormat:='YYYY-MM-DD';      
 //AFormatSettings.ShortDateFormat:='MM-DD-YYYY'; //can be also used        
 DataValue:=FormatDateTime(AFormatSettings.ShortDateFormat,DateTym);          

end;        

Don't affraid that system AFormatSettings will be change . If you exit from
this procedure system setting for AFormatSettings will be came back to default setting , because it is local variable

0
votes

had the same problem. apparently i wished to convert date to string in this format "yyyy-MM-dd" and i couldnt...apparently a simple work around is that you just change your system date format to your desired output format...