Stef Merlijn
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
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
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;
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;
ASKER
Do you have an example on how to do it?
The daterange might start with a friday and end with a thuesday.
The daterange might start with a friday and end with a thuesday.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry didnt see the disabled part.
Just change the checked property to enabled
Just change the checked property to enabled
ASKER
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
..I just love waisting my time :p
ASKER
I really sorry rfwoolf.
I closed the question to early as it seems.
I closed the question to early as it seems.
ASKER
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
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.
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.
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)
ASKER
Bye the way:
mikelittlewood also used DayOfWeek which I believe is also using DateUtils.
Maybe rfwoolf can sleep better now. :-)
mikelittlewood also used DayOfWeek which I believe is also using DateUtils.
Maybe rfwoolf can sleep better now. :-)
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(MyDateSelector