Solved

Convert string to Date Time

Posted on 2004-03-27
11
589 Views
Last Modified: 2010-04-05
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);
 
 
0
Comment
Question by:jennifere
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 10695163
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10695395

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


0
 

Author Comment

by:jennifere
ID: 10695513
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..
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:esoftbg
ID: 10695609
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
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10695617
Excuse me but replace
>   DT := EncodeDate(Y, M, D);
with
      DT := EncodeDate(2000+Y, M, D);
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10695632
and:
>    SY := Copy(DS, 6, 2);
with
    SY := Copy(DS, 7, 2);
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 250 total points
ID: 10695664
Your value is not zero, believe me. If it was 0, then

 DateTimeToStr(verifiedDate);

would give you 12/31/1899

and its not, its giving you your value

try this

var
 hrs, mins, secs, msecs: Word;

 DecodeDate (verifiedDate, Ahrs, Amins, Asecs, Amsecs);

 now look at this values during a break

this should verify that verifiedDate is not zero

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10695712
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

 
0
 
LVL 17

Expert Comment

by:mokule
ID: 10695724
Shane
May be it is clear may not to sombody but checking verifiedDate should be done after excecution of the assignment
verifiedDate := ....
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10695911
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
0
 

Author Comment

by:jennifere
ID: 10696107
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 91
Delphi: Connect to running MS Outlook 4 117
how to update exe applicatio from internet ? 6 86
shape, triangle, dbctrlgrid 3 17
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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