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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare dates.

Status
Not open for further replies.

TheSaviour

Programmer
Sep 14, 2002
13
0
0
GB
I have 2 dates and I want to compare them and get the number of days in between them. The dates start off as strings in the DD/MM/YYYY format,
 
TheSaviour,

Off the top of my head, something like this should work:

Code:
uses DateUtils;
// ...
procedure TForm1.Button1Click(Sender: TObject);
var
   dtBeg,
   dtEnd : tDatetime;
   intDays : integer;

begin
   dtBeg := encodeDate( '1980', '01', '01' );
   dtEnd := encodeDate( '2002', '09', '14' );

   intDays := daysBetween( dtEnd, dtBeg );

   messageBoxFmt( 'There are %d days between %s and %s.',
                  [ intDays, dateTimeToStr( dtBeg ),
                    dateTimetoStr( dtEnd ) ] );

end;

Hope this helps...

-- Lance
 
If they are in string format you should

DtStart := StrToDate(stringDate1);
DtEnd := StrToDate(stringDate2);

then DaysBetween

or

to use the encodedate you would have to parse your date strings into separate Month, Day and Year, and convert to integers. Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Or to make it even simpler.

var
Start, Finish : TDate;
Days : Variant;
begin
Start := StrToDate(YourString);
Finish := StrToDate(YourOtherString);
Result := Finish - Start;
end;

Result now equals the number of days difference between the two dates. Arte Et Labore
 
Messed that up!

Switch the days in the variable declarations for Result and it works.

Darn my stupid brain!

Arte Et Labore
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top