[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
13
Medium Priority
?
276 Views
Last Modified: 2012-05-06
Haw can I count  weekend's  in the month .
Maybe some function .
0
Comment
Question by:Peev711
  • 7
  • 6
13 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
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

by:Geert Gruwez
ID: 23726741
that was presuming monday = 1
0
 

Author Comment

by:Peev711
ID: 23726978
for I := 0 to DaysInMonth(Dt)-1 do
here gave me some error
/ incompatible types word and tdatetime /
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 38

Expert Comment

by:Geert Gruwez
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

by:Peev711
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

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

Author Comment

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

Author Comment

by:Peev711
ID: 23727676
Sorry my MISTAKE for a last post
0
 
LVL 38

Expert Comment

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

Author Comment

by:Peev711
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

by:Geert Gruwez
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;

Open in new window

0
 
LVL 38

Accepted Solution

by:
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;

Open in new window

0
 

Author Comment

by:Peev711
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question