Solved

Calculating # of Days Between 2 Dates

Posted on 1998-01-24
10
297 Views
Last Modified: 2010-04-04
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
Comment
Question by:d4jaj1
  • 4
  • 3
  • 3
10 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1357483
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

by:d4jaj1
ID: 1357484
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

by:inter
ID: 1357485
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Author Comment

by:d4jaj1
ID: 1357486
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

by:ZifNab
ID: 1357487
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

by:inter
ID: 1357488
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

by:ZifNab
ID: 1357489
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

by:d4jaj1
ID: 1357490
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!

Igor, answer the question & I will grade you.  Thanks for your efforts.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357491
d4jaj1, doesn't bother me at all!
0
 
LVL 5

Accepted Solution

by:
inter earned 20 total points
ID: 1357492
Friends,

Ok, Thanks, Byte,

Igor
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 123
Delphi: ForceDirectory plain function 7 42
TEMBEDDEDWB how can i change its user agent ? 8 70
Delphi...Split view - idea? 1 90
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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