Solved

Calculating # of Days Between 2 Dates

Posted on 1998-01-24
10
294 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

by:d4jaj1
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:inter
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
d4jaj1, doesn't bother me at all!
0
 
LVL 5

Accepted Solution

by:
inter earned 20 total points
Comment Utility
Friends,

Ok, Thanks, Byte,

Igor
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now