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

x
?
Solved

Difference in days between two dates

Posted on 2002-04-25
7
Medium Priority
?
169 Views
Last Modified: 2010-04-04
Hi Folks,

I have two dates.  For example, 07/04/1990 and 25/12/2002 (as you can see, they're in the format of DD/MM/YYYY).  I need to find out how many DAYS have passed since the first date to the second date.

I'm sure there's an easy way of doing this, it's just I can't be bothered thinking about it tonight :)

Thanks,

Stu
0
Comment
Question by:Stuart_Johnson
7 Comments
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6968161
Oh yeah, if 50 points ain't enough, I'll up it.  I think this is an easy Q., but if it's not, I'm happy to double or triple it.

Stu
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 6968170
convert it to tdatetime
(there are various functions to do this)
and then just take the difference

count of days:= date2-date1;

(sorry no delphi on hand,
therfore no sample,
but so it goes in theory)

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6968176
appendix,
the result will be of double-type,
just use trunc to transform the
result into an integer
0
Technology Partners: 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 6

Expert Comment

by:DrDelphi
ID: 6968206
straight from my website (www.drdelphi.com)



var yesterday,today:Tdatetime;
    dayspassed:integer;
begin
today:=strtodate('05/28/1998');
yesterday:=strtodate('05/27/1998');
dayspassed:=(round(today))-(round(yesterday));
showmessage(inttostr(dayspassed));
{Notice that we use the round function here..
This is to get the numbers of DAYS.. if we were looking for the
fractional part of the day as well, we would use a real datatype
for dayspassed and showmessage as FloatTostr(daypassed)}

end;



Good luck!!

0
 
LVL 3

Expert Comment

by:VSF
ID: 6968240
If you are using Delphi6 declare dateutils on your uses clause... on this unit you will find lots of interesting procedures for date handling!
Such as:

function IsInLeapYear(const AValue: TDateTime): Boolean;
function IsPM(const AValue: TDateTime): Boolean;
function IsValidDate(const AYear, AMonth, ADay: Word): Boolean;
function IsValidTime(const AHour, AMinute, ASecond, AMilliSecond: Word): Boolean;
function IsValidDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond,
  AMilliSecond: Word): Boolean;
function IsValidDateDay(const AYear, ADayOfYear: Word): Boolean;
function IsValidDateWeek(const AYear, AWeekOfYear,                    {ISO 8601}
  ADayOfWeek: Word): Boolean;
function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth,     {ISO 8601x}
  ADayOfWeek: Word): Boolean;
function WeeksInYear(const AValue: TDateTime): Word;                  {ISO 8601}
function WeeksInAYear(const AYear: Word): Word;                       {ISO 8601}
function DaysInYear(const AValue: TDateTime): Word;
function DaysInAYear(const AYear: Word): Word;
function DaysInMonth(const AValue: TDateTime): Word;
function DaysInAMonth(const AYear, AMonth: Word): Word;
function Today: TDateTime;
function Yesterday: TDateTime;
function Tomorrow: TDateTime;
function IsToday(const AValue: TDateTime): Boolean;
function IsSameDay(const AValue, ABasis: TDateTime): Boolean;


Hope it helps!
VSF
www.victory.hpg.com.br
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6968315
Hi Meikl,

Thanks heaps :)  Worked a treat.

Thanks to everyone else too for you quick responses!

Stu
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 6968332
DrDelphi,

Meikl's code was the first posted that solved the problem, but it was yours that I actually used.  So, in all fairness, here's some points for you as well.  Thanks a lot!

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=20293500

Cheers,

Stu
0

Featured Post

Technology Partners: 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

782 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