Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calculating # of Days Between 2 Dates

Posted on 1998-01-24
10
Medium Priority
?
302 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 80 total points
ID: 1357492
Friends,

Ok, Thanks, Byte,

Igor
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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