Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert String DateTime to TDateTime

Status
Not open for further replies.

Nordlund

Programmer
Jul 17, 2001
458
SE
Hi folks.
This thread is a follow up to this thread:
I have problems to convert another dateformat:

I've tried this approach:
Code:
  GetLocaleFormatSettings(0, F);
  F.ShortDateFormat := 'mmm/dd/yyyy';
  F.ShortTimeFormat := 'hh:nn:ss';
  F.DateSeparator := '/';
  F.TimeSeparator := ':';
  d := StrToDateTime('Aug/17/2005 12:12:12', F);

...but the function throws an error.
(The dateformat cannot be altered)

Solutions?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
The Delphi 7 help says that the string containing the date must consist of two or three numbers. This means that you cannot use the name of a month such as 'Aug'.

So
Code:
d := StrToDateTime('08/17/2005 12:12:12', F);
would work.

If it is necessary for you to convert a text month then you will need to do a simple look up of the ShortMonthNames array to convert a text month in to a number.

Andrew
Hampshire, UK
 
Hi there.

Ok. That was what I suspected....
Why didn't I read the help? :)


Working codesnippet solving this problem:
Code:
function MyStrToDate(DateStr: String): TDateTime;
var
  F: TFormatSettings;
  s: String;
  i: Integer;
begin
  GetLocaleFormatSettings(0, F);
  F.ShortDateFormat := 'mmm/mm/yyyy';
  F.ShortTimeFormat := 'hh:nn:ss';
  F.DateSeparator := '/';
  F.TimeSeparator := ':';

  s := Copy(DateStr, 1, Pos('/', DateStr) -1);
  Result := 0;
  try
    for i := Low(F.ShortMonthNames) to High(F.ShortMonthNames) do
      if SameText(s, F.ShortMonthNames[i]) then
      begin
        Result := StrToDateTime(StringReplace(DateStr, s, FormatFloat('00', i), [rfIgnoreCase]), F);
        Break;
      end;
  except
    Result := 0;
  end;

  if Result = 0 then
    Raise Exception.Create('Could not convert the date ' +DateStr);
end;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
Being incredibly pedantic, if the date / time you wanted to convert was Dec/30/1899 00:00:00 then your function would raise an exception instead of giving the correct value of 0. Also your setting of the ShortDateFormat seems to be wrong.
A slightly simpler implementation could be
Code:
function MyStrToDate(DateStr: String): TDateTime;
var
  F: TFormatSettings;
  s: String;
  i: Integer;
  separator: integer;
begin
  GetLocaleFormatSettings(0, F);
  F.ShortDateFormat := 'm/d/yyyy';
  F.ShortTimeFormat := 'hh:nn:ss';
  F.DateSeparator := '/';
  F.TimeSeparator := ':';

  separator := Pos(F.DateSeparator, DateStr);

  if separator = 0 then
    Raise Exception.Create('No date separator in date ' + DateStr);

  s := Copy(DateStr, 1, separator-1 );

  i := High(ShortMonthNames);

  while ( i >= Low(ShortMonthNames) ) and not SameText ( s, ShortMonthNames[i] ) do
    dec ( i );

  s := IntToStr(i) + Copy ( DateStr, separator, length(DateStr) );
  if i >=  Low(ShortMonthNames) then
    result := StrToDateTime ( s, F )
  else
    Raise Exception.Create('Could not convert the date ' + DateStr)
end;

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top