Link to home
Start Free TrialLog in
Avatar of jennifere
jennifere

asked on

Convert string to Date Time

I am reading a date field 'mm/dd/yy' from a table.  I need to be able to perform calculations on it to determine if it is older than 90 days.  When I pull it from the table using the .AsDateTime, it comes back with the value '0'.  However when I convert it to a string, it works fine and displays the date.  I am assuming this is because my date is not formatted correctly including the time 'MM/DD/YY HH:MM:SS.'  Is there a way I can get around this and just store and work with the date only?  

verifiedDate := Form1.tblAgents.fieldbyname('EmailVerified').AsDateTime;
strVerifiedDate := DateTimeToStr(verifiedDate);
 
 
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

Avatar of shaneholmes
shaneholmes


It shouldn't be zero, place a condition break here

--> verifiedDate := Form1.tblAgents.fieldbyname('EmailVerified').AsDateTime;
      strVerifiedDate := DateTimeToStr(verifiedDate);

then run, and when it breaks there, place mouse over verifieddate, look at the hint, see if it shows its value.

Shane


Avatar of jennifere

ASKER

I did place a condition break there-  this is how I determined the value '0'.  I have no idea why it is saying 0.  I checked the field in the table and it is '12/30/03'.

Basically- I'm at this point.  I have the date stored as a string value: strVerifiedDate.  I just need to find a way to convert this string to a date.  I have tried 'verifiedDate := StrToDate(strVerifiedDate);' but I am having no luck..
try this:

function  Str_To_Date(DS: string): TDate;
var
  D:      Word;
  M:      Word;
  Y:      Integer;
  I:      Integer;
  DT:     TDate;
  SD:     string;
  SM:     string;
  SY:     string;
begin
  DT := 0;
  try
    SD := Copy(DS, 4, 2);
    SM := Copy(DS, 1, 2);
    SY := Copy(DS, 6, 2);
    Val(SD, D, I);
    Val(SM, M, I);
    Val(SY, Y, I);
    DT := EncodeDate(Y, M, D);
  finally
    Result := DT;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  DT:     TDate;
begin
  DT := Str_To_Date('03/27/04');
  ShowMessage(FormatDateTime('MM/dd/yy', DT));
end;

emil
Excuse me but replace
>   DT := EncodeDate(Y, M, D);
with
      DT := EncodeDate(2000+Y, M, D);
and:
>    SY := Copy(DS, 6, 2);
with
    SY := Copy(DS, 7, 2);
ASKER CERTIFIED SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also try doing this as well

var
 DT1: TDateTime;
 
 DT1:= EncodeDate(StrToDate('12/31/1899'),AHrs, AMins, ASecs, AMSecs);
 
 if verifiedDate > DT1 then its not zero!

Shane

 
Shane
May be it is clear may not to sombody but checking verifiedDate should be done after excecution of the assignment
verifiedDate := ....
right, i assumed jennifer would step through at least one line past the break, i guess i should have been more clear about it... thanks for waking me up - <SMILE>

Shane
Oops... I was looking at the wrong field.  My date field actually says '12/30/1899'.  Maybe this is explains it....    Thanks- that makes sense now.