DaysBetween returns the number of days between two specified TDateTime values.

Fractional or part days are not counted, so the function will return a whole integer value only.

For example, DaysBetween reports the difference between 1st February 11:59 PM and 2nd February 11:58 PM as zero (0) days between because the day stops a second short of an entire day.

The ordering of the two dates doesn't matter - the number of months will not be negative.

Declaration: Function DaysBetween(const ANow: TDateTime; const AThen: TDateTime): Integer;

Examples follow.

procedure OnMapEvent(var Value:Variant);
var
vYear, vDate, vTween, vTween2 : TDateTime;
vStrYear, vStrDate : string;
vBetwix : integer;
begin
//Result is 4 whole days
vBetwix := DaysBetween(Date, Date+4);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 2 whole days
vStrDate := '01-10-2023';
vStrYear := '03-10-2023';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := DaysBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 0 whole days - 1 second short of a day
vTween := EncodeDateTime(2023, 10, 01, 23, 59, 59, 999);
vTween2 := EncodeDateTime(2023, 10, 02, 23, 58, 59, 999);
LogInfo(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', vTween));
LogInfo(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', vTween2));
vBetwix := DaysBetween(vTween, vTween2);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 1 whole day - seconds match
vTween := EncodeDateTime(2023, 10, 01, 23, 59, 59, 999);
vTween2 := EncodeDateTime(2023, 10, 02, 23, 59, 59, 999);
LogInfo(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', vTween));
LogInfo(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', vTween2));
vBetwix := DaysBetween(vTween, vTween2);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
end;