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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is date between 2 others

Status
Not open for further replies.

GotItOn

Programmer
Mar 28, 2012
3
GB
Hello,
In the system I am currently making I require to know if 1 date is between 2 others.

The 2 dates it must be between are part of a record, and the date which is tested for being between them is a date selected using the calendar.

As an algorithm:

Date1, SmallDate, BigDate <-- TDateTime
between <-- boolean

if (Date1 = SmallDate) or (Date1 = BigDate) then
between = true
else if Date1 is between SmallDate and BigDate then
between = true
else
between = false


I have tried doing it using DaySpan and CompareDate functions, but I couldn't get either to work and am out of ideas.

Thanks :)
 
Assuming that your SmallDate and BigDate are only holding valid Dates, as opposed to valid Date and Times, then you should use

Trunc(<MyDateField>)
Code:
Function TForm1.IsDateBetween(SmallDate, BigDate, CompareDate: TDateTime): Boolean;
begin
   Result := (trunc(SmallDate) <= trunc(CompareDate)) and 
             (trunc(BigDate) >= trunc(CompareDate));
end;
 
You can also use the function DateInRange of the DateUtils unit:
Code:
function DateInRange(ADate: TDate; AStartDate, AEndDate: TDate; AInclusive: Boolean = True): Boolean;

Example:
Code:
Between := DateInRange(Date1, SmallDate, BigDate);

I hope it's useful.


Imoveis em Guarulhos
 
Thanks for the reply guys :D

Neither seemed to work, the first way it said operator not applicable to this operand type.

The second way it said DateInRange wasn't found, even though DateUtils is being used :/

Thanks though
 
Are you sure SmallDate and BigDate are actually declared as TDateTime, and if so, how are you setting the values for them? You mention the compare date is set via a calendar, how are you assigning the value there also?
 
For the moment, the compare date is only set when the calendar is changed. It is a pre-created delphi calendar that's used, and the date from that is simply obtained by:
Calendar1.CalendarDate, and is called in the procedure Calendar1Change(Sender: TOBject).

The small date and big date are both loaded from a textfile and are both TDateTime variables, however they have no time assigned to them, only a date.

The data is being loaded to them, as they are displayed elsewhere in the project.

Thanks for your help :D
 
As I see it, the possibilities are that either
- somehow you are calling IsDateBetween before SmallDate, BigDate or CompareDate has valid values.

Call IsDateBetween from the OnChange Event after you set CompareDate's value, and make sure that by then, you also have parsed your input file, and assigned SmallDate and BigDate.

- Big and Small date do not have valid Date value derived from your file.

Make sure BigDate and SmallDate are both declared as TDateTime and use the EncodeDate function ( to ensure the value you have is a valid date (i.e. not 31 Feb ...)

- Incorrectly using the IsDateBetween function.

IsDateBetween Returns a boolean, so you can use it in an if statement, or assign it to a boolean field.

If IsDateBetween(SDt, BGDt, CmpDt) then
...

or
var
X: Boolean;
begin
X := IsDateBetween(SDt, BGDt, CmpDt);
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top