在Visual Basic中计算两个日期之间的工作日数

发布于 2024-11-25 01:18:31 字数 125 浏览 0 评论 0原文

我是一名 SQL 人员,但我需要一个函数来计算 VB.NET 中两个日期之间的工作日数。我不需要担心假期。不幸的是我的尝试是徒劳的。非常感谢

这将进入 Reporting Service 2008 R2 中的自定义代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抱猫软卧 2024-12-02 01:18:31

试试这个。我修改了我一直在使用的现有函数。这将在 SSRS 2008 中工作。请注意,如果您对此更熟悉,您也可以用 C# 编写代码,并且在部署之后,只需引用报告中的程序集即可。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 

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 
染墨丶若流云 2024-12-02 01:18:31

您不需要检查这些日期之间的每一天是否是工作日。

如果有 n 天,则有 int(n / 7) 完整周,每周包含 5 个工作日,因此为 5 * int(n / 7)工作日。

然后,您需要检查剩余部分周的天数(0..6 天)。

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).

南笙 2024-12-02 01:18:31

“这是我的版本 vb.net 2013,它可以工作

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

”谢谢 Gabe

'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

人间不值得 2024-12-02 01:18:31

这可能会帮助其他人寻找这个。该函数不会日复一日地循环。

    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;
    }

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;
    }
野侃 2024-12-02 01:18:31

我有一个表达式可以计算两个日期之间一周中特定日期的数量。因此,如果您在周一、周二...周五添加金额,您将得到工作日的金额。

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

[DayofWeek] 是代表一天的整数:1 - 星期日; 2 - 星期一

因此,计算 dteStartDateTimedteEndDateTime 之间星期一数量的表达式为:

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

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)) 
2024-12-02 01:18:31

我认为这可能会有所帮助

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

在这里,我返回了“wkend”,它是函数中变量的周末部分。或者,您可以将返回值更改为“wkday”,即所选日期之间的工作日数。
我希望这有帮助。不过它对我有用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文