Solved

Convert date

Posted on 2014-03-03
6
405 Views
Last Modified: 2014-03-03
Hi.

I have excel document where the name of the sheets is the date the sheet was created. But some of the sheets have a date format like this: dd/mm/yyyy and I want it like this: yyyy/mm/dd. The name of the sheets in a Listbox is added by using ADOConnection.GetTableNames(ListBox1.Items,false);

How can I convert the dateformat to the right one?
0
Comment
Question by:QC20N
  • 4
  • 2
6 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39900417
in delphi simply use FormatDateTime
FormatDateTime( 'yyyy/mm/dd', DateTimeValue)

can you actually use slashes in the file name, if not :
> FormatDateTime('yyyymmdd', DateTimeValue)

excel ???
0
 

Author Comment

by:QC20N
ID: 39900430
Yes, but how? :)

If you see in line 15 I add TableName to the listbox.

procedure TFrmMain.BtnADOConnectXLSClick(Sender: TObject);
var strConn :  widestring;
begin
  strConn := 'Provider=Microsoft.ACE.OLEDB.12.0;' +
             'Data Source=' + 'C:\path\Project Pro_SE.xlsx' + ';' +
             'Extended Properties="Excel 12.0 xml;HDR=YES";';
  AdoConnection1.Connected:=False;
  AdoConnection1.ConnectionString:=strConn;
  ADOConnection1.CursorLocation := clUseServer;
  ADOConnection1.LoginPrompt := false;
  ADOConnection1.Mode := cmShareDenyNone;
  ADOConnection1.Provider := 'Microsoft.ACE.OLEDB.15.0';
  try
    ADOConnection1.Open;
    ADOConnection1.GetTableNames(listbox1.Items,false);
  except
    ShowMessage('Unable to connect to Excel, make sure the workbook exist!');
    raise;
  end;
  AdoQuery1.CursorLocation:=clUseServer;
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39900443
how did the tablenames get in there ?

if you don't change them at that level  you'll need:
A > a translation from the orginal format to your new display format
B > that filename won't exist anymore, so you can't do anything with it in the next step

change the original filename formats by setting up a rule when entering the file names.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39900451
do you have some samples of correct and wrong filenames ?

without those samples,  it's guessing at what the interpretation converter should do
0
 

Author Comment

by:QC20N
ID: 39900463
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28363030.html

Just below the accepted solution in the link there is file. If that is was you are asking. :)
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 39900667
well that's basically swap-a-little
if the first '-' is at position 5 then it's ok, otherwise swap

besides your format is not with slashes but with minusses !!!

TempList := TStringList.Create;
try
  ADOConnection1.GetTableNames(TempList,false);
  ConvertDateList(TempList);
  listbox1.Items.Assign(TempList);
finally 
  TempList.Free;
end;

Open in new window


procedure TFormXXX.ConvertDateList(List: TStrings);
var I: Integer;
  Temp: string;
begin
  for I := 0 to List.Count-1 do
  begin
    Temp := List[I];
    if Pos('-', Temp) <> 5 then 
    begin
      Temp := Copy(Temp, 7, 4) + '-' + Copy(Temp, 4, 2) + '-' + Copy(Temp, 1, 2);
      List[I] := Temp;
    end;
  end;
end;

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 84
Downloading email attachments 2 79
shape, triangle, dbctrlgrid 3 17
Browsing a TTreeView in Delphi 5 6
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question