4
votes

I have two dates: 1. Feb 1, 2013 2. Now. So there is a difference of 2 days in between 2 dates. How can I get this difference of days in delphi programmatically?

2
I'd say the difference is three days (4−1=3), but you can always add/subtract one if you'd like. - Andreas Rejbrand
@AndreasRejbrand - okay - user1556433

2 Answers

10
votes

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)));
2
votes

The TDateTime is a float format where the integer part represents the number of days while the zecimal part represents the time (as a fraction of 24h).

So if you want to get a date that's tow days from today, you just add 2 to the original. If you you've got two dates and you want to compute 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 would say it's more readable:

D := IncDay(D, 2);