Hi everyone..
I have problem here..
i want to calculate total days using vb.net..this calculation not including public holiday. i have calendarstartdate, calendarenddate and txttotalabsent. my problem here how to capture public holiday and how to calculate total days.
please help me :sad: ..

Dani AI

Generated

— first decide whether the date range is inclusive (count both start and end) and whether “public holiday” means a maintained list of dates (recommended) or rules (e.g., nth weekday, observed-on-Monday). As asked: clarify if only Sundays or all weekend days are excluded. ’s pointer can help, but here is a compact, practical approach.

Algorithm (summary)

  • Normalize the inputs (use Date.Date to drop time; ensure start <= end).
  • Compute weekday-only days by counting full weeks and remaining days (fast, no per-day loop).
  • Subtract any holiday dates that fall on weekdays and inside the range.
  • Store holidays as explicit dates per year (or an ObservedDate column) so “observed” rules don’t have to be re-calculated at runtime.

Efficient VB.NET function (counts inclusive days, excludes weekends and weekday holidays):

Function GetBusinessDays(startDate As DateTime, endDate As DateTime, holidays As IEnumerable(Of DateTime)) As Integer
    If startDate.Date > endDate.Date Then
        Dim tmp = startDate
        startDate = endDate
        endDate = tmp
    End If

    Dim totalDays As Integer = (endDate.Date - startDate.Date).Days + 1
    Dim wholeWeeks As Integer = totalDays \ 7
    Dim businessDays As Integer = wholeWeeks * 5

    Dim leftover As Integer = totalDays Mod 7
    Dim startDow As Integer = CInt(startDate.DayOfWeek) ' Sunday = 0
    For i As Integer = 0 To leftover - 1
        Dim dow = (startDow + i) Mod 7
        If dow <> CInt(DayOfWeek.Saturday) AndAlso dow <> CInt(DayOfWeek.Sunday) Then businessDays += 1
    Next

    Dim holidaysInRange As Integer = 0
    For Each h As DateTime In holidays
        Dim hd = h.Date
        If hd >= startDate.Date AndAlso hd <= endDate.Date Then
            If hd.DayOfWeek <> DayOfWeek.Saturday AndAlso hd.DayOfWeek <> DayOfWeek.Sunday Then holidaysInRange += 1
        End If
    Next

    Return businessDays - holidaysInRange
End Function

Tips and caveats

  • If ranges are small or clarity is preferred, a day-by-day loop (check weekday + lookup holiday) is acceptable.
  • Prefer storing holidays as concrete dates (and an ObservedDate) rather than trying to encode every rule.
  • Watch inclusive/exclusive behavior, time components, and culture-specific calendars.
  • If holidays repeat (e.g., first Monday in September), pre-compute them per year and save results to the holiday table for fast queries.

Recommended Answers

All 2 Replies

Do you want to exclude just sundays? or you already have a list of public holidays? how are u defining a public holiday?


SHould do exactly what you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.