Solved

Date String to TDate

Posted on 2008-11-03
8
731 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 36

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 36

Expert Comment

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

ShowMessage(FormatDateTime('MM/DD/YYYY', ConvertDate('November 3, 2008')));
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 36

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 36

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

21 Experts available now in Live!

Get 1:1 Help Now