[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# count  weekend's  in the month

Posted on 2009-02-24
Medium Priority
276 Views
Haw can I count  weekend's  in the month .
Maybe some function .
0
Question by:Peev711
• 7
• 6

LVL 38

Expert Comment

ID: 23726719
just count the number of certain days in a month of a year:

use DateUtils

function DaysInMonth(aDay, aMonth, aYear: Word): Integer;
var I: Integer;
Dt: TDateTime;
begin
Result := 0;
Dt := EncodeDate(aYear, aMonth, 1);
for I := 0 to DaysInMonth(Dt)-1 do
if DayOfWeek(Dt + I) = aDay then
Result := Result + 1;
end;

use like
NumSundays := DaysInMonth(7, 7, 2009); // = 4
0

LVL 38

Expert Comment

ID: 23726741
that was presuming monday = 1
0

Author Comment

ID: 23726978
for I := 0 to DaysInMonth(Dt)-1 do
here gave me some error
/ incompatible types word and tdatetime /
0

LVL 38

Expert Comment

ID: 23727044
lol, same function name, silly me
change first line

function NumDaysInMonth(aDay, aMonth, aYear: Word): Integer;
var I: Integer;
Dt: TDateTime;
begin
Result := 0;
Dt := EncodeDate(aYear, aMonth, 1);
for I := 0 to DaysInMonth(Dt)-1 do
if DayOfWeek(Dt + I) = aDay then
Result := Result + 1;
end;
0

Author Comment

ID: 23727190
Function dont work corect for
examples Mart has a 5 Sundays but the function return 4
DaysInMonth(7,3, 2009); // = 4

0

Author Comment

ID: 23727250
NumDaysInMonth(7,3, 2009); // = 4
Sorry
0

Author Comment

ID: 23727454
why
I := EncodeDate(2009,2, 1);
DayOfWeek(I) ; = 4
but
I := EncodeDate(2009,2, 1);
LongDayNames[DayOfWeek(I)]; = sunday
0

Author Comment

ID: 23727676
Sorry my MISTAKE for a last post
0

LVL 38

Expert Comment

ID: 23727782
it depends what first day of  week is
0

Author Comment

ID: 23727899
I see that ,but still not work for me
I wont some function that returns how many saturdays and sundays has 0 specific month
0

LVL 38

Expert Comment

ID: 23730969
so you want to count the whole weekends ?
``````function WholeWeekendsInMonth(aMonth, aYear: Word): integer;
const DaySunday   = 1;
var I: Integer;
Dt: TDateTime;
begin
Result := 0;
Dt := EncodeDate(aYear, aMonth, 1);
for I := 0 to DaysInMonth(Dt)-1 do
if (DayOfWeek(Dt + I) = DaySunday) and (MonthOf(Dt+I-1) = aMonth) then
Result := Result + 1;
end;
``````
0

LVL 38

Accepted Solution

Geert Gruwez earned 500 total points
ID: 23730992
i put a monthcalendar on my form and a onclick event and used the above functions:
``````function NumDaysInMonth(aDay, aMonth, aYear: Word): Integer;
var I: Integer;
Dt: TDateTime;
begin
Result := 0;
Dt := EncodeDate(aYear, aMonth, 1);
for I := 0 to DaysInMonth(Dt)-1 do
if DayOfWeek(Dt + I) = aDay then
Result := Result + 1;
end;

function WholeWeekendsInMonth(aMonth, aYear: Word): integer;
const DaySunday   = 1;
var I: Integer;
Dt: TDateTime;
begin
Result := 0;
Dt := EncodeDate(aYear, aMonth, 1);
for I := 0 to DaysInMonth(Dt)-1 do
if (DayOfWeek(Dt + I) = DaySunday) and (MonthOf(Dt+I-1) = aMonth) then
Result := Result + 1;
end;

procedure TForm1.MonthCalendar1Click(Sender: TObject);
var aMonth, aYear: Word;
begin
aMonth := MonthOf(MonthCalendar1.Date);
aYear := YearOf(MonthCalendar1.Date);
ShowMessage(Format('WholeWeekends: %d, Sundays: %d, Saturdays: %d',
[WholeWeekendsInMonth(aMonth, aYear),
NumDaysInMonth(1, aMonth, aYear),
NumDaysInMonth(7, aMonth, aYear)]));
end;
``````
0

Author Comment

ID: 23731111
Thanks its work perfekt naw i see the first day of a week is a sunday a sevent saturday.
Thanks again.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi projâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can eâ€¦
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
###### Suggested Courses
Course of the Month19 days, 15 hours left to enroll