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

between a date range 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I thought I had a clue, but I guess I was wrong.....I need a specific piece of code to execute if today is between October 15 and March 15, no matter the year and I'm having problems figuring out how to do it the easiest! I can figure out lots of complicated formatting of dates and checking, but there HAS to be a better way...

any ideas?

Thanks!

leslie
 
Here's one...
Code:
function TForm1.IsBetween: Boolean;
//if today is between October 15 and March 15, no matter the year
var Target: TDateTime;
begin
  Target:= StrToDate(Edit1.Text);
  case MonthOf(Target) of
    10:     result:= DayOf(Target) >= 15;
    11, 12,
    01, 02: result:= true;
    03:     result:= DayOf(Target) <= 15;
    else    result:= false
  end; //case
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsBetween then
    Label2.Caption:= 'TRUE'
  else
    Label2.Caption:= 'FALSE'
end;
Worked for me. Could be nested if statement. I used case to make it clear what's going on. Let me know if it fit's the bill.

Roo
Delphi Rules!
 
Sometimes twisting the data around a bit to make a continuous range can help for date sets. Like for this (untested):

Code:
if month <= 3 then
  month := month + 12;
if month in [10..15] then
  run program;
 
Good tip Glenn. That's thinking outside the box for sure, but misses mid-month to mid-month. It would take a bit more if Les had asked for "if today is between Noon of October 15 and Noon of March 15, no matter the year.", in which case I'd use TimeOf(Target) >= 0.5
 
True, I didn't get the day into the example (all that would have taken was a simple comparison). I figured the month was the likely challenge.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top