Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtracting Dates - paradox???

Status
Not open for further replies.

setitup

IS-IT--Management
Jul 30, 2003
17
ZA
Hi there,
I've got a problem, when subtracting two dates, say from Date and Time picker I get the correct result. But when subtracting a Date from a paradox database date field I get incorrect results. Upon investigating this I found that delphi converts dates to doubles, say 10 June 2004 will be 3427 and 11 Junbe 2004 will become 3428. My problem, when I get the date from the database,
dateAndTimePicke:=FieldInDataBase('field').AsDate then the
double from dateAndTimePicker (doubl:=dateandtimepicker, becomes 3427.45362837. Hope you follow. The result has a lot of unexplained decimals. If I want to establish if the 10'th smaller than the 9'th it doesn't return the correct result.

Any opinions or ideas on this. I finally got this sorted out by using the StrToFloat and then reconverting the result without the extra decimals.

Any input will be regarded as useful.
Thank you
 
The clue is in the name DateAndTimePicker. The unexplained decimals are the time part.

So, for example, 3427.5 would be noon on 10 June 2004 and 3428.75 would be 6pm on 11 June 2004.

The Trunc function removes the decimals from the DateTime to give an integer which is purely a date.
Code:
var
  dateonly: integer;
...
begin
...
  dateonly := Trunc ( DateTimePicker1.Date );
...
end;

Andrew
Hampshire, UK
 
I think you can also use:

DateTimePicker1.Date

to get/set just the date portion

Leslie
 
If you look at the source code (or help) for TDateTimePicker you will see that the Date property is defined as a TDate which in turn is defined as TDateTime which in turn is defined as a Double.

A Double is a real number (contains digits on either side of the decimal point) and so the Date property in TDateTimePicker can and usually does contain a time part as well as the date.

I therefore recommend that if you just want the date you should use the Trunc function to remove the time part so that the date can then be used for comparisons with other dates (such as those from most databases).



Andrew
Hampshire, UK
 
Thanx,
I did not know of the trunc function. I used a series of conversion (to stringFF etc) to get it right. Thanx your support is valued.
 
Andrew,

What do I need to do to get just the TIME portion of the TDateTime?

Thanks,

leslie
 
Figured it out, it's the FRAC(TDateTime) function!!

les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top