Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Quick way to determine days in date-range

Hi,

On my form I have 7 checkboxes. Each representing on weekday.
cbMa, cbDi, cbWo, cbDo, cbVr, cbZa and cbZo. (mon-sun)

On the form the user can enter a date-from and a date-to.
If the date-range contains less then 7 days, than checkboxes representing the days of the week that are not within the date-range much be disabled.

Can anybody give me a quick way to accomplish this?
Regards, Stef
Avatar of rfwoolf
rfwoolf
Flag of South Africa image

Add Dateutils to your uses clause
and then use DaysBetween which requires two TDateTime values.
Here's what the helpfile says

[Delphi] function DaysBetween(const ANow: TDateTime; const AThen: TDateTime): Integer;
Call DaysBetween to obtain the difference, in days, between two TDateTime values. DaysBetween counts only whole days. Thus, DaysBetween reports the difference between Dec 31, 1999 11:59 PM and Jan 1, 2000 11:58 PM as 0 because the difference is one minute short of an entire day.
 
====
So you might say
DaysNumber := DaysBetween(MyDateSelector1.date, MyDateSelector2.date);
Of course there is an added element of logic you'll have to use.
Let's say the first day selector selects a day that falls on a Wednesday, and the second is the Friday. You will obviously only want the checkboxes from Wednesday Thursday and Friday to show.
For that you can use DayOfTheWeek from dateutils, which returns a number from 1 to 7.
Here's the syntax:
DayOfTheWeek(const AValue: TDateTime): Word;
Avatar of Stef Merlijn

ASKER

Do you have an example on how to do it?
The daterange might start with a friday and end with a thuesday.
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry didnt see the disabled part.
Just change the checked property to enabled
Thank you.

uses
  DAteutils
 
Let us assume that you're using 2 tDateTimePicker's called tDateTimePicker1 and TDateTimePicker2 to let the user select a start and finish date and that they then click a button (or you do this on the OnChange event)
 
We can use Dateutils to determine the day of the week of our start date and end date.
 
we'll declare 2 variables for our start date and end date
 
var
  StartDay : integer;
  EndDay : integer;
  i : integer; //also need a counter
 
begin
 
//First of all, if the date range is larger than 7 days, then all checkboxes are checked
if DaysBetween(DatetimePicker1.Date, DateTimePicker2.Date) >= 7 then
begin
  cbMa.checked := true;
  cbDi.checked := true;
  cbWo.checked := true;
  cbDo.checked := true;
  cbVr.checked := true;
  cbZa.checked := true;
  cbZo.checked := true;
end
else
begin
  //Get the start day and end day, taking into account that the
  //stupid user may set the start date that is later than the end date.
  If DateTimePicker1.Date > DateTimerPicker2.Date then
  begin
    StartDay := DayOfTheWeek(DateTimePicker2.Date);
    EndDay := DayOfTheWeek(DateTimePicker1.Date);
  end
  else
  begin
    StartDay := DayOfTheWeek(DateTimePicker1.Date);
    EndDay := DayOfTheWeek(DateTimePicker2.Date);
  end;
  
  i := StartDay;
  while i <> EndDay do
  begin
    case i of
      1 : cbMa.checked := true;
      2 : cbDi.checked := true;
      3 : cbWo.checked := true;
      4 : cbDo.checked := true;
      5 : cbVr.checked := true;
      6 : cbZa.checked := true;
      7 : cbZo.checked := true;
   end;
    i := i + 1;
    if i > 7 then i := 1;
  end;
end;
 
//NOTE: I forget how to end case statements - I think you put an end; at the end - but anyways, you could always do 7 if ... then statements instead

Open in new window

..I just love waisting my time :p
I really sorry rfwoolf.
I closed the question to early as it seems.
rfwoolf:
Your solution isn't checking the checkbox for the last day.
A small change of line 44 will do it though.
was: while i <> EndDay do
now: while i <> EndDay + 1 do

My bad. Yes that makes sense :)
Sorry rfwoolf, I started to write my answer even before your first post.
By the time I had finished and posted I saw the rest of the messages.
It's nobody's fault :)
And hey, you managed to do yours without dateuls, which is always remarkable, although I personally prefer the conciseness of the dateutils stuff.
Yeah I could probably tidy my code up a bit to be honest but it was a quick rush job :o)
Bye the way:
mikelittlewood also used DayOfWeek which I believe is also using DateUtils.
Maybe rfwoolf can sleep better now. :-)