 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Apr 03, 2007 8:11 am Post subject: Finding weekends between two date |
|
|
Helo,
Is there any function to find weekends (Saturday,sunday) between two
dates ?
For example : I will give two dates and the function will give number
of holidays (Saturday,sunday) between two dates.
N:=weekendbetween(startdate,enddate)
Thanks
Mücahit |
|
| Back to top |
|
 |
yannis Guest
|
Posted: Tue Apr 03, 2007 8:11 am Post subject: Re: Finding weekends between two date |
|
|
mcelikag (AT) ebim (DOT) net wrote :
| Quote: | Helo,
Is there any function to find weekends (Saturday,sunday) between two
dates ?
For example : I will give two dates and the function will give number
of holidays (Saturday,sunday) between two dates.
N:=weekendbetween(startdate,enddate)
Thanks
Mücahit
|
well as far as I can see this is a simple mater of
(EndDate - StartDate) Div 7
If I am wrong there will be others correcting me I hope.
Regards
Yannis. |
|
| Back to top |
|
 |
Oliver Guest
|
Posted: Tue Apr 03, 2007 6:17 pm Post subject: Re: Finding weekends between two date |
|
|
Is there any function to find weekends (Saturday,sunday) between two
dates ?
On what database? Some databases can return the day of the week, which you
could take advantage of.
After the last few weeks, I would argue that in IT there's no such thing as
a weekend anymore...
Oliver Townshend |
|
| Back to top |
|
 |
Wayne Niddery [TeamB] Guest
|
Posted: Tue Apr 03, 2007 7:35 pm Post subject: Re: Finding weekends between two date |
|
|
yannis wrote:
| Quote: |
well as far as I can see this is a simple mater of
(EndDate - StartDate) Div 7
If I am wrong there will be others correcting me I hope.
|
Pick a random starting date and count the Sat/Sun's in the next 9 days.
There can 2, 3, or 4.
--
Wayne Niddery - Winwright, Inc (www.winwright.ca)
"Bandwagons are like streetcars, there'll be another along in a few
minutes." |
|
| Back to top |
|
 |
yannis Guest
|
Posted: Tue Apr 03, 2007 8:57 pm Post subject: Re: Finding weekends between two date |
|
|
Wayne Niddery [TeamB] presented the following explanation :
| Quote: | yannis wrote:
well as far as I can see this is a simple mater of
(EndDate - StartDate) Div 7
If I am wrong there will be others correcting me I hope.
Pick a random starting date and count the Sat/Sun's in the next 9 days. There
can 2, 3, or 4.
|
Well I mend it as a starting point but since you asked ;P here is a
small function I cooked up just now for this as far as I have tested it
works as expected but be warned that I have tested it for only five
minutes. I can't see any cases where this would fail if you do find one
I will be most interested in hearing it.
function CalculateWeekEndDays (const FromDate, ToDate
:TDateTime):Integer;
var
Days : Integer;
Weeks : Integer;
DaysLeft : Integer;
Wday : Integer;
ExtraDays : Integer;
WeekEndDays : Integer;
begin
if ToDate > FromDate then
Days := Trunc(ToDate - FromDate)
else
Days := Trunc(FromDate - ToDate);
Weeks := Days div 7;
DaysLeft := Days mod 7;
WeekEndDays := Weeks * 2;
if ToDate > FromDate then
WDay := DayOfWeek(FromDate)
else
WDay := DayOfWeek(ToDate);
ExtraDays := Wday - 1 + DaysLeft;
if ExtraDays >=6 then Inc(WeekEndDays);
if ExtraDays >=7 then Inc(WeekEndDays);
Result := WeekEndDays;
end;
Regards
Yannis. |
|
| Back to top |
|
 |
John Herbster Guest
|
Posted: Tue Apr 03, 2007 10:02 pm Post subject: Re: Finding weekends between two date |
|
|
<mcelikag (AT) ebim (DOT) net> wrote
| Quote: | Is there any function to find weekends (Saturday,sunday)
between two dates ?
For example : I will give two dates and the function will
give number of holidays (Saturday,Sunday) between two
dates. N:=weekendbetween(startdate,enddate)
|
Mücahit, Your definition is lacking a bit of detail. For
example, must the Saturday and Sunday be together to
be counted. And of what value is the answer, if it does
not account for the multitude of other holidays? If you
really need a practical answer, Dr. John Stockton, who
frequently posts in these delphi groups used to have
references to practical solutions. of the holiday problem
on his web pages.
Nevertheless, here below is how an engineer might
solve your question as a puzzle. It should be easier for
a real programmer. Regards, JohnH
procedure TForm1.MonthCalendar1Click(Sender: TObject);
{ Drop two TMonthCalendars and a TMemo box on a form
and connect up both OnClick events to this handler.
Pay particular attention to the meaning of "between". }
var d1, d2,
NbrCompleteWeeksBetween,
NbrExtraDaysBetween,
FirstExtraDayNbr,
NbrExtraWeekendDaysBetween,
NbrWeekEndDaysBetween: integer;
begin
d1 := trunc(MonthCalendar1.Date);
d2 := trunc(MonthCalendar2.Date);
NbrCompleteWeeksBetween := (d2 - d1) div 7;
NbrExtraDaysBetween := (d2 - d1) mod 7;
FirstExtraDayNbr := (d1 - 2) mod 7; {Monday = 0}
NbrExtraWeekendDaysBetween := Max(0,Min(2,
(FirstExtraDayNbr + NbrExtraDaysBetween - 5)));
NbrWeekEndDaysBetween := NbrCompleteWeeksBetween*2 +
NbrExtraWeekendDaysBetween;
Memo1.Lines.Add(Format('%D %D %D %D %D %D %D',
[d1,d2,
NbrCompleteWeeksBetween,
NbrExtraDaysBetween,
FirstExtraDayNbr,
NbrExtraWeekendDaysBetween,
NbrWeekEndDaysBetween]));
end; |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|