Get total no of working days in a year

I wanted to get Total number of working days in a year (exclude Saturday and Sunday)

Can anyone suggest me a solution for this?

Hi @Nisha_K21

Can you try this

Enumerable.Range(0, (New DateTime(2024,12,31) - New DateTime(2024,1,1)).Days + 1).
Count(Function(x) New DateTime(2024,1,1).AddDays(x).DayOfWeek <> DayOfWeek.Saturday And 
                  New DateTime(2024,1,1).AddDays(x).DayOfWeek <> DayOfWeek.Sunday)

Regards,

@Nisha_K21

intWorkingDays = Enumerable.Range(0, 366).Count(Function(x) (New DateTime(2024, 1, 1).AddDays(x).DayOfWeek <> DayOfWeek.Saturday And New DateTime(2024, 1, 1).AddDays(x).DayOfWeek <> DayOfWeek.Sunday))

you can modify the year and number of days

1 Like

Hi @Nisha_K21

Try this Code.

You can replace “Now.Year” with the year of your choice.

startDate = New DateTime(Now.Year, 1, 1)
endDate = New DateTime(Now.Year, 12, 31)
totalWorkDays = Enumerable.Range(0, (endDate - startDate).Days + 1).
                    Count(Function(d) startDate.AddDays(d).DayOfWeek <> DayOfWeek.Saturday AndAlso 
                                      startDate.AddDays(d).DayOfWeek <> DayOfWeek.Sunday)

If you have further queries do let me know. If it solves your question, do mark it as a solution.

Thank You
Happy Automation

In addition to above (lets dynamize it by variables)

  • we let generate the Years Day count, as we also want to handle Leap Years (instead of Hardcoded 366)
  • Lets simplifiy and avoid redundand code parts for the SUN, SAT Check

grafik

Enumerable.Range(0,new GregorianCalendar().GetDaysInYear(2024) + 1).Where(Function (x) Not {0,6}.Contains(#01/01/2024#.AddDays(x).DayOfWeek)).Count()

ensure that System.Globalization is added to the namespace imports

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.