Get date format from a ClientDataset

Marco Gasi
Marco Gasi used Ask the Experts™
on
Hi all.
I'm writing a small app - I only write small apps :-) - to write an sql dump file from a ClientDataset. I'm now trying to convert dates. So, assunming in MySql I have to insert values in the following format

yyyy-mm-dd

I wish to know how to get the format date are stored within ClientDataset. Since this format depends on locxal settings I can have in date field a string like 'dd/mm/yyyy', or 'mm/dd/yyyy' and so on. I should be able to get the actual date format within ClientDataset in order to convert that date in the format 'yyyy-mm-dd' (I solved yet the problem of slashes and dashes using AnsiReplaceText function)

Thanks to all for any help.

Marco
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AshokSr. Software Engineer

Commented:
Use getdateformat
It is a windows API.

http://winapi.freetechsecrets.com/win32/WIN32GetDateFormat.htm

HTH
Ashok

AshokSr. Software Engineer

Commented:
procedure TForm1.Button1Click(Sender: TObject);
var
  lSystemTime : TSystemTime;
  lBuffer : array[0..64] of Char;
  lErrorCode : Integer;
begin
  FillChar(lSystemTime, sizeof(TSYSTEMTIME), #0);
  lSystemTime.wDay := 25;
  lSystemTime.wMonth := 8;
  lSystemTime.wYear := 2004;

  if GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, @lSystemTime, nil,
    lBuffer, sizeof(lBuffer)) <> 0 then
    ShowMessage(string(lBuffer))
  else
    ShowMessage(SysErrorMessage(GetLastError));
end;
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
Thanks for your reply.

Let me explain better. My app doesn't know anything about ClientDataset it's going to migrate in mysql dump file. It get field type iterating through the dataset opened by user. If it encounter date field it has to know which formt that value use. Theorically, user could be swedish but it could try to convert a dataset filled with an US format. I can't assume that dataset format corresponds to system locale. So, if ClioentDataset doesn't hold any information about date field format, maybe I have to use some regular expression to understand wich format date field use...

I'm wrong?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

AshokSr. Software Engineer

Commented:
If user could be swedish, is he/she using computer in USA?

If so, ClientDataset will not hold any format info by default within the same date field.
If you know how many different formats you will be supporting, you could add one extra field and have format stored as string.

HTH
Ashok
 
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
How is the CLientDataSet created?

mlmcc
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
I'm tryin to write a progam to allow to anyone to create a mysql dump file from any ClientDataset. It should be an utility for Delphi programmers who need to migrate some MyBase database to mysql, so I can't to know how a ClientDataset is created, nor I can create any extra field.

Perhaps I could let the user tells date format used by selecting it from a combobox... Or I could try to get string and trhen chenge its format using regular expression. The problem is not the creation opf the dump file: this is yet done. But when I tried to import with PhpMyAdmin, date format has not been recognized and all dates have been wrote as '0000-00-00' in the mysql database.

Any idea?
AshokSr. Software Engineer

Commented:
You could add Calculated field to ClientDataset at runtime.

Then use calculated field to determine which format to use to show date.

Ashok
Freelancer
Top Expert 2010
Commented:
Sorry, but it was a trivial task: iterating throught the dataset, I can just use

value := QuotedStr(FormatDateTime('yyyy-mm-dd', StrToDate(Fields.Fields[x].AsString)));

where value is the string that will be written in the value clause of sql statement INSERT

I'm going to ask to close this question. I'm very sorry for having wasted your time: hope youìll excuse me.

Thanks for having tried to help me.
Marco GasiFreelancer
Top Expert 2010

Author

Commented:
I solved my problem by myself because I understood that it was trivial.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial