Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Date Manipulation

Posted on 1998-08-07
9
438 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:CSGPC
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1361652
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);

Where
  Years, Month, Days: Byte;

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

--Matvey


0
 

Author Comment

by:CSGPC
ID: 1361653
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.)
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361654
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);
begin
  Table1DifDate.AsFloat := Now - Table1SaleDate.AsDateTime;
end;

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);
begin
  Value := FloatToStr(Now - Table1SaleDate.AsDateTime);
end;


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:Matvey
ID: 1361655
-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?
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361656
Why TimeToStr???

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

then
Now-SaleDate = 1

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

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1361657
-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.
0
 
LVL 3

Expert Comment

by:vladika
ID: 1361658
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.
It's NOT DATE.
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.

0
 

Author Comment

by:CSGPC
ID: 1361659
Matvey did not answer the question - answer by Vladika acceptable. Would like to rate him C.
0
 
LVL 3

Accepted Solution

by:
vladika earned 100 total points
ID: 1361660
My "official" answer see below
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 166
indy Tmemory stream non latin charcter issue 2 121
Simple Delphi Question 9 96
Convert MS Word document to a PDF file 9 90
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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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