How to check if a date matches a date range (both lines)?

I have two lines, for example 23.01-2002 - 23.06.2002 and 23.01-2002 - 23.06.2002 .

How do I know if a date in the first row matches dates in the second row?

What i thought

 dateString := '05.04.2002'; dateRangeString := '23.01-2002 - 23.06.2002'; date := StrToDate( dateString ); rangeStart := StrToDate( LeftStr(dateRangeString, 10) ); rangeEnd := StrToDate( RightStr(dateRangeString, 10) ); 

Now from there I do not know what to do!

+4
source share
3 answers

You can use the System.DateUtils device and its DateInRange function:

 var dStart, dEnd, d2Test: TDate; begin dStart := StrToDate('25/07/2012'); dEnd := StrToDate('29/07/2012'); d2Test := StrToDate('26/07/2012'); if DateInRange(d2Test, dStart, dEnd) then ShowMessage('In range!'); 

You can also check the fourth parameter of this function ( AInclusive: Boolean = True ) ... depending on your needs ...

+8
source

A TDateTime variable is essentially a double variable, and the order between two date and time values ​​(considered date and time values) is the same as the order between values ​​considered to be real numbers.

 procedure TForm4.FormCreate(Sender: TObject); var d1, d2, d: TDate; begin d1 := StrToDate('2012-07-25'); d2 := StrToDate('2012-07-29'); d := StrToDate('2012-07-26'); if (d1 <= d) and (d <= d2) then ShowMessage('In range!'); end; 

Of course, using Math , you can also write

  if InRange(d, d1, d2) then ShowMessage('In range!'); 
+3
source

The only thing I would like to add to this is that if the dates are formatted in YYYY-MM-DD format, you can simply compare them to determine if it is within the range. No need to convert them to TDateTime first.

 R1 := '2012-01-01'; R2 := '2012-01-31'; D := '2012-01-15'; if (D >= R1) and (D <= R2) then // within range 
+2
source

All Articles