Good Morning,

Please, I need a way to calculate the working days of the current month.

How can I do it?

Solution

Hello,

I don't think any component does the work for you. Holidays change between countries, cities, religions, moon phase.... it is impossible to please everyone.

NewDate(GetYear(Currdate()),Month(Currdate())1)
Will put you on the first day of the month.

Do a loop incrementing days (using AddDays()) until you change the month.
For each day try DayOfWeek() that will return 0 for Sunday, 1 for Monday,... until 6 for Saturday.
Count the ones you want.


It is ugly, but it works.


maybe add it to Date Handling component?

Solution

If you prefer you can try this forge component: CalculateBusinessDay

Hi,

Perhaps you could combine what Nuno Reis has said with a logical to turn it less "ugly".

Depending on which days you will consider as a work day (I am considering Mon to Fri), you can calculate Trunc(DiffDays(start, end) / 7) * 2 to get all Sat and Sun. After, you can evaluate the same code Nuno Reis has suggested to verifiy the remaining days.

Sure, if you need to exclude holidays It will be necessary to do more than this.

This is just a hint in order to play a little more. It is a good way to learn!!!

Not exactly Wilbert.

March has 9 weekend days, May will have 10. It is not constant because it depends on when it starts.

You're right! "Not exactly". I just wanted to provoke a moment of reasoning and study for the best implementation through self-learning. :)

But...

Both months (march and may) have at least 8 days out (28 days / 7 * 2).

Plus:

- March: 29-31 (verifying each day) has one more day out (29 Sunday)

- May: 29-31 (verifying each day) has two more days out (30 Saturday and 31 Sunday)


This way, we can always count 8 and verify the days after 28!!!

Yes, that is the most performant option. In the first 28 days, there are always 8 weekend days. Check only the final 0, 1, 2 or 3 days.

Even with holidays, you just need to read the holidays list and see if

1) it is on the current month;

2) it is a weekend (same working days) or not (-1 day).