Solved

Calculating # of Days Between 2 Dates

Posted on 1998-01-24
10
296 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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