Date Manipulation

How do I calculate the number of days between the current date and a date field.
I am using Delphi 3 with QuickReport 2.0g
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

For example:

The date in the field is
(Table1.fields[i] as TDateField).AsDateTime

Th other date value is
Date1: TDateTime

The difference is calculated as if you would substract two numbers:

Dif := Date1 - (Table1.fields[i] as TDateField).AsDateTime

You can extract the number of days from Dif by using

DecodeDate(Dif, Years, Month, Days);

  Years, Month, Days: Byte;

-Days will show then the number of days between the two.


CSGPCAuthor Commented:
This is how you would do it in Delphi. I need to manipulate QuickReport to produce the anwers. (Currently the report produces a label instead of the number of days.)
When you use DecodeDate Days will be in 1..DaysInMonth (DaysInMonth = 28, 29, 30 or 31)
not the number of days between the two.

You do not need call DecodeDate.
The integral part of a Dif value is the number of days between two.
(see help on TDateTime).

I found two ways how to PRINT
the number of days between the current date and a date field.

1) Create calculated field DifDate (Integer or Float)
and write OnCalcFields handler as

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
  Table1DifDate.AsFloat := Now - Table1SaleDate.AsDateTime;

set QRDBText component into report and set property DataField = DifDate

2) set QRLabel component into report
and write OnPrint handler as

procedure TForm1.QRLabel1Print(sender: TObject; var Value: String);
  Value := FloatToStr(Now - Table1SaleDate.AsDateTime);

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

-It's TimeToStr, not FloatToStr Vladika...

Sorry, I didn't understand that you need to put it in the Report. Does it works now with Vladika's sugestion?
Why TimeToStr???

  Now = 7-aug-98
  SaleDate = 6-aug-98

Now-SaleDate = 1

FloatToStr will print 1 (It's right answer, isn't it?)
and TimeToStr will print 00:00:00

-Sorry, I meant DateToStr, not TimeToStr...
If it was more than 31 days? Then it won't be so easy. You'll have to use DecodeDate then, and add Month multiplied by 30-31.
Why DateToStr???

From example above
DateToStr will print 31.12.1899 (if ShortDateFormat = 'dd/mm/yyyy')

I cannot understand why do you want to use DecodeDate procedure?
The DecodeDate procedure breaks the value specified as the Date parameter
into Year, Month, and Day values.
But Dif = Date1 - Date2 IS NUMBER of days between Date1 and Date2.
Dif can be negative (if Date1 < Date2).
But how write in help
If the given TDateTime value is less than or equal to zero,
the year, month, and day return parameters are all set to zero.

So, Date1-Date2 IS NUMBER of days between Date2 and Date1.
You do not need call DecodeDate at all.
Of course, to print this value (Dif) you may use not only FloatToStr function,
but others too.
I use FloatToStr only for example.

CSGPCAuthor Commented:
Matvey did not answer the question - answer by Vladika acceptable. Would like to rate him C.
My "official" answer see below

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.