Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-05-2006, 09:04 AM
jorgen jorgen is offline
Member
 
Join Date: Dec 2001
Posts: 80
Default Subtracting from TDateTime value ?

If I have two TDateTime variables, like:


CurrDate:=Date;
ArrivalDate:=StrToDate(18.02.1995);


...and I wanna subtract arrival date from current date to some third variable:


Interval:=Date - ArrivalDate


... how do I get a valid result to be used for other DateTime routines, while this simple subtraction doesn't give result like 12.10.0005 (TDateTime starts with 12/30/1899).


I really need TDateTime value, 'cause other DateTime routines (like IncDay) should be performed on that result.


Thnx for all suggestions.
Reply With Quote
  #2  
Old 02-05-2006, 09:07 AM
gLes gLes is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,273
Default RE: Subtracting from TDateTime value ?

There are numerous functions for this in the SysUtils unit, all of them end with the word Span, lookup DaySpan for example in the help file and you'll find the rest...

Hope that helps

gLes
Reply With Quote
  #3  
Old 02-05-2006, 04:24 PM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default RE: Subtracting from TDateTime value ?

"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 fraction of a 24 hour day that has elapsed."

(Quoted from Delphi 4 help...)


Subtracting one TDateTime variable from another results in a number which is the difference between the days. The whole part of the number is the difference in days, and the fractional part is the difference
in time.


For instance, if var1 = 1/5/2000 12:00 pm and var2 = 1/3/2000 6:00 am, then var1 - var2 = 2.25, where 2 is the number of days and .25 is a quarter of a day, or 6 hours.


You should evaluate the result as a float (or an integer if you just want the day difference and don't care about fractional days).


Returning the result into another TDateTime field would result in a date of 01/01/1900 06:00:00 am, because you are setting the date with 2.25, and 01/01/1900 06:00:00 am is exactly 2.25 days after 12/30/1899
12:00:00 am.

so,

If three variables -- var1,var2 and var3 are all TDateTime type, and var3 = var1 - var2,

then var3 will simply contain the difference between var1 and var2, expressed in days. If var3 = 3.5 then the difference between var1 and var2
is 3.5 days (= 3 days and 12 hours).



If this helps, please accept as answer!

Thanks!

UnSysApps
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:56 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.