In VB, calculating the 5th working day of each month

In VB, what encoding could I use to calculate the 5th working day of each month? And if the 5th day is a holiday, to rise one day.

+3
source share
4 answers

You will need a list of vacation dates with which you can compare. You will need to create and store this list separately. You did not say which version of VB (VBA? VB.NET?), But in VB.NET you could do:

Dim datevalue As DateTime = New DateTime(DateTime.Year, DateTime.Month, 1)
Dim dayIsFound As Boolean = False
Dim workDays As Integer

workDays = 1
While Not dayIsFound
    If ( dateValue.DayOfWeek <> DayOfWeek.Saturday _ 
        And dateValue.DayOfWeek <> DayOfWeek.Sunday _ 
        And Not HolidayList.Contains( dateValue )  _ 

        workDays = workDays + 1
    End If

    If  index >= 5 Then
        dayIsFound = True
    Else
        dateValue = dateValue.AddDays(1)
    End If
End While

Technically, you can build an algorithm that determines the main holidays based on federal recommendations (in the USA), but this is difficult and may not correspond to the holidays of the company you are building this component.

+1
source

: , -, , .

, , , Google / . , . http://www.cpearson.com/EXCEL/holidays.htm

VB Weekday WeekdayName ( ) , , , .

+1

" " . , .

+1

You need to count every valid working day until you get to the fifth. For instance.

index = 0
Do
  If dateValue.DayOfWeek <> DayOfWeek.Saturday _ 
      AndAlso dateValue.DayOfWeek <> DayOfWeek.Sunday _ 
      AndAlso Not HolidayList.Contains( dateValue ) Then

     index = index + 1
     If index >= 5 Then _
        Exit Do
  End If
  dateValue = dateValue.AddDays(1)
Loop
+1
source

Source: https://habr.com/ru/post/1738508/


All Articles