Solved

Date String to TDate

Posted on 2008-11-03
8
739 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

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