Count the number of weekdays between two dates in visual basic

送分小仙女□ 提交于 2020-01-21 09:52:10

问题


I'm a SQL guy, but I need a function to calculate the number of weekdays between two dates in VB.NET. I don't need to worry about holidays. My attempts unfortunately have been futile. Much appreciated

This will go in custom code in Reporting Service 2008 R2.


回答1:


Try this. I modified an existing function that I've been using. This will work in SSRS 2008. Note, that you can also write your code in C#, if you're more comfortable with that, and after deploying it, just reference the assembly from the report.1

public Shared Function Weekdays(ByRef startDate As Date,  ByRef endDate As Date   ) As integer
    dim numWeekdays as Integer
    dim totalDays as Integer
    dim WeekendDays as Integer
    numWeekdays = 0
    WeekendDays = 0

    totalDays = DateDiff(DateInterval.Day, startDate , endDate ) + 1

    for i as integer = 1 to totalDays

        if DatePart(dateinterval.weekday,startDate) = 1 then
            WeekendDays = WeekendDays + 1
        end if
        if DatePart(dateinterval.weekday, startDate) = 7 then
            WeekendDays = WeekendDays + 1
        end if
            startDate = DateAdd("d", 1, startDate)
    next

    numWeekdays  = totalDays - WeekendDays 

    return numWeekdays  
End Function 



回答2:


You don't need to check whether every single day between those dates is a weekday.

If there are n days, then there are int(n / 7) complete weeks, each containing 5 weekdays, so that's 5 * int(n / 7) weekdays.

You then need to check the days of the remaining partial week (0..6 days).




回答3:


'This is my version vb.net 2013 and its works

Private Sub enddate_ValueChanged(sender As Object, e As EventArgs) Handles enddate.ValueChanged
        Dim countsun As Integer = 0
        Dim countsat As Integer = 0
        Dim nonholiday As Integer = 0
        Dim totalDays = (enddate.Value - startdate.Value).Days
        For i = 0 To totalDays
            Dim Weekday As DayOfWeek = startdate.Value.Date.AddDays(i).DayOfWeek
            If Weekday = DayOfWeek.Saturday Then
                countsat += 1
            End If
            If Weekday = DayOfWeek.Sunday Then
                countsun += 1
            End If
            If Weekday <> DayOfWeek.Saturday AndAlso Weekday <> DayOfWeek.Sunday Then
                nonholiday += 1
            End If
        Next
        lblSumHeadCount.Text = nonholiday & " Day(s)"
    End Sub

'Thanks Gabe




回答4:


This may help someone else looking for this. This function doesn't loop day by day.

    public static double WorkDays(DateTime start, DateTime end)
    {
        var delta = end.AddDays(1) - start;
        var fullWeeks = (int)(delta.TotalDays / 7);
        var workDays = fullWeeks * 5;

        var partialWeekDays = delta.TotalDays % 7;
        if (partialWeekDays > 0)
        {
            var startWeekday = start.DayOfWeek;
            var endWeekday = end.DayOfWeek;

            if (startWeekday == DayOfWeek.Sunday || startWeekday == DayOfWeek.Saturday)
                partialWeekDays--;
            if (startWeekday > DayOfWeek.Sunday && startWeekday < DayOfWeek.Saturday &&
                startWeekday > endWeekday)
                partialWeekDays--;
            if ((endWeekday == DayOfWeek.Sunday || endWeekday == DayOfWeek.Saturday) && endWeekday != startWeekday)
                partialWeekDays--;
            if (endWeekday > DayOfWeek.Sunday && endWeekday < DayOfWeek.Saturday &&
                startWeekday > endWeekday)
                partialWeekDays--;
        }

        return workDays + partialWeekDays;
    }



回答5:


I have an expression that will calculate amount of specific day of week between two dates. So if you add amount on Mondays, Tuesdays ... Fridays you will get the amount of week days.

=SUM(int((DatePart("d",Fields!dteEndDateTime.Value) - Weekday(DateAdd("d",1-**[DayofWeek]**,Fields!dteEndDateTime.Value)) - DatePart("d",Fields!dteStartDateTime.Value) + 8)/7))

[DayofWeek] is an integer representing a day: 1 - Sunday; 2 - Monday

So an expression that will calculate amount of Mondays between dteStartDateTime and dteEndDateTime is:

=SUM(int((DatePart("d",Fields!dteEndDateTime.Value) - Weekday(DateAdd("d",1-2,Fields!dteEndDateTime.Value)) - DatePart("d",Fields!dteStartDateTime.Value) + 8)/7)) 



回答6:


I think this may help

Public Shared Function WeekEndsBetweenDates(ByVal StartDate As Date, ByVal EndDate As Date) As Integer
    Dim wkday, wkend As Integer
    For i As Integer = 0 To DateDiff(DateInterval.Day, StartDate, EndDate)

        If DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Saturday Or DateAdd(DateInterval.Day, i, StartDate).DayOfWeek = DayOfWeek.Sunday Then
            wkend += 1
        Else
            wkday += 1
        End If
    Next
    Return wkend
End Function

Here, i returned "wkend" which is the weekend part of the variables in the function. Alternatively, you can change the returned value to "wkday" which is the number of weekdays between the selected dates. I hope this helps. It works for me though



来源:https://stackoverflow.com/questions/6710296/count-the-number-of-weekdays-between-two-dates-in-visual-basic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!