Solved

# Calculating # of Days Between 2 Dates

Posted on 1998-01-24
294 Views
How do I calulate the number of days between two dates? It would be nice if the code took into account leap years.  Thanks.
0
Question by:d4jaj1
• 4
• 3
• 3

LVL 5

Expert Comment

Friend,
You can use the following functions (from RXlib which is free):

function DaysBetween(Date1, Date2: TDateTime): Longint;
begin
Result := Trunc(Date2) - Trunc(Date1) + 1;
if Result < 0 then Result := 0;
end;

Sincerely,
Igor

0

LVL 3

Author Comment

This code does not work unless the two dates are in the same month and year.  For example, the code below returns 1, when obviusly the there is more that one day betweeen these two dates.  Any suggestions?

showmessage(inttostr(daysbetween(12/16/97,1/15/97)));
0

LVL 5

Expert Comment

Are you using Delphi 1.0 my friend, The following info is got from delphi 2.0 help. Please examine

TDateTime: type Double;
----------------------

Description
-----------
TDateTime is the type used by date and time routines to hold date and time values.
Delphi stores date and time values in the TDateTime type. The integral part of a TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of a TDateTime value is the time of day.
Following are some examples of TDateTime values and their corresponding dates and times:
0               12/30/1899 12:00 am
2.75               1/1/1900 6:00 pm
-1.25               12/29/1899 6:00 am
35065               1/1/1996 12:00 am
To find the fractional number of days between two dates, simply subtract the two values. Likewise, to increment a date and time value by a certain fractional number of days, simply add the fractional number to the date and time value.

!!!!NOTE:!!!!Delphi 1.0 calculated the date from year 1 instead of from 1899. To convert a Delphi 1.0 date to a Delphi 2.0 date, subtract 693594.0 from the Delphi 1.0 date. The date format changed to be more compatible with OLE 2.0 Automation.

Ok?
Sincerely,
Igor
0

LVL 3

Author Comment

Sorry, I'm not a Delphi expert - yet.  I'm not clear as to how I would subtract and fractional value (float) from a date (tdatetime)  You you have some same code?  I'm using D2.
0

LVL 8

Expert Comment

Search for date routines in the delphi help!

StrToDate : to convert string to date;

for getting year, month, day out of tdatetime, use :

procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);

0

LVL 5

Expert Comment

Ok, Friend,

The answer I had given is correct, it substract second from the first so, you should be careful that Date2 > Date1. However inyour example Date1>Date which means that DaysBetween returns 0.

The logic of tdatetime is simple. it is actually a floating point number which packs all date and time info in. The integral part of it gives the NUMBER OF DAYS PASS SINCE 12/30/1899. So if we have to dates in TDateTime format as

d1 = x.y  (here x is the integral, y is the floating part)
d2 = k.l  (here k is the integral, l is the floating part)

In delphi THE DAYS BETWEEN d2 and d1 is = k - x+1, which exacly what I have given before.

Igor
0

LVL 8

Expert Comment

Hi all, Igor, d4jaj1,

d4jaj1, Igor is right, normally, can't find anthing wrong in his code. But don't forget! Igor's functions needs TDateTime variables! and it seems that in your test you gave strings, so what you've to do is the following :

showmessage(inttostr(daysbetween(StrToDate('12/16/97'),StrToDate('1/15/97'))));

If this doesn't suits your question, you've to work with the date routines in delphi.
0

LVL 3

Author Comment

I aplogize for the confusion.  What I should have done was add a comment to Igor's answer instead of Rejecting it. Zif, I am rejecting your answer because the original answer by Igor was correct (although a little confusing).  I'm doing this to be fair to Igor, not because your additions were incorrect.  Hope I don't offend you!

0

LVL 8

Expert Comment

d4jaj1, doesn't bother me at all!
0

LVL 5

Accepted Solution

inter earned 20 total points
Friends,

Ok, Thanks, Byte,

Igor
0

## Featured Post

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.