How to get the difference between dates in the number of days in Delphi?

I have two dates: 1. February 1, 2013 2. Now. Thus, the difference between the two dates is 2 days. How can I get this days difference in delphi programmatically?

+4
source share
2 answers

Use the DaysBetween function found in DateUtils :

 var d1, d2: TDate; begin d1 := EncodeDate(2013, 02, 01); d2 := EncodeDate(2013, 02, 04); ShowMessage(IntToStr(DaysBetween(d2, d1))); 
+8
source

TDateTime is a floating point format where the integer part represents the number of days, while the zecimal part represents the time (as part of 24h).

So, if you want a date that is towing from today, just add 2 to the original. If you have two dates and you want to calculate the distance in days, use DaysBetween , as Andreas suggests.

Example:

 var D:TDateTime; begin D := EncodeDate(2013, 2, 1); D := D + 2; // Adds two days. end; 

You can also use the IncDay function from DateUtils to do the same; Some say this is more readable:

 D := IncDay(D, 2); 
+2
source

All Articles