Solved

Date String to TDate

Posted on 2008-11-03
8
740 Views
Last Modified: 2010-04-21
I need to convert a date string to a TDate

How to convert  'November 3, 2008'   to 11/03/08

thanks
0
Comment
Question by:geocoins-software
  • 4
  • 2
  • 2
8 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 22867626
You want to avoid this at all cost, so if you do this, you probably went somewhere wrong earlier.

Why do you want to do this?
0
 

Author Comment

by:geocoins-software
ID: 22867676
This is a string returned from parsing an html file.

<td valign="top">October 28, 2008      </td></tr>

thanks
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 22867702
here is the code using the USER_LOCALE
function ConvertDate(aDateString: string): TDateTime;
var aMonth, aYear, aDay: Word;
  I: Integer;
  Temp: string;
  fmt: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_USER_DEFAULT, fmt);
  aMonth := 1;
  Temp := Copy(aDateString, 1, Pos(' ', aDateString)-1);
  for I := 2 to 12 do
    if SameText(Temp, fmt.LongMonthNames[I]) then
    begin
      aMonth := I;
      Break;
    end;
  Temp := Copy(aDateString, Pos(' ', aDateString)+1, 2);
  if Temp[2] = ',' then
    Delete(Temp, 2, 1);
  aDay := StrToInt(Temp);
  Temp := RightStr(Trim(aDateString), 4);
  aYear := StrToInt(Temp);
  Result := EncodeDate(aYear, aMonth, aDay);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(FormatDateTime('DD/MM/YYYY', ConvertDate('November 3, 2008')));
end;

Open in new window

0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22867714
ow, the FormatDateTime should be

ShowMessage(FormatDateTime('MM/DD/YYYY', ConvertDate('November 3, 2008')));
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22867718
oops again and the year to YY

ShowMessage(FormatDateTime('MM/DD/YY', ConvertDate('November 3, 2008')));
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 22867724
is the website always returning the date in the same language, is this the same language is the machine your app runs on (always)?
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22867726
don't forget in the implementation to add the

uses StrUtils
0
 

Author Closing Comment

by:geocoins-software
ID: 31512710
THANK YOU!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Mydac connection data base issue 3 166
Strange behavior when a form is closed 6 61
Working with hours 3 65
MS Access from Delphi 31 60
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
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…

828 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