Solved

Date String to TDate

Posted on 2008-11-03
8
734 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
 
LVL 37

Expert Comment

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

ShowMessage(FormatDateTime('MM/DD/YYYY', ConvertDate('November 3, 2008')));
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 76
Delphi XE10 Round Image 2 79
Open a URL with Internet Explorer in a new tab (not a new window) 1 74
Multiple image collision 13 69
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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now