vb.net - 计算日期添加,但仅限工作日

标签 vb.net date

我想简单地使用内置的 dateadd 函数来计算新日期,但要考虑到只应计算工作日(或可以说是“工作日”)。

我想出了这个简单的算法,它不关心假期等。我已经用一些简单的日期对此进行了测试,但如果可以以更好的方式完成,我希望得到一些输入。

此示例假设一周有 5 个工作日,即周一至周五,其中一周的第一天是周一。这里使用的日期格式是 d-m-yyyy,示例以 2009 年 10 月 1 日为开始日期进行计算。

这是简单的形式:

Dim d_StartDate As DateTime = "1-10-2009"
Dim i_NumberOfDays As Integer = 12
Dim i_CalculateNumberOfDays As Integer 

If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then
    i_CalculateNumberOfDays = i_NumberOfDays
Else
    i_CalculateNumberOfDays = i_NumberOfDays + (Int(((i_NumberOfDays + (7 - d_StartDate.DayOfWeek)) / 5)) * 2)
End If

MsgBox(DateAdd(DateInterval.Day, i_CalculateNumberOfDays, d_StartDate))

我尝试用下面的代码来解释:

''' create variables to begin with
Dim d_StartDate as Date = "1-10-2009"
Dim i_NumberOfDays as Integer = 5

''' create var to store number of days to calculate with
Dim i_AddNumberOfDays as Integer 


''' check if amount of businessdays to add exceeds the 
''' amount of businessdays left in the week of the startdate
If i_NumberOfDays > (5 - d_StartDate.DayOfWeek) Then


    ''' start by substracting days in week with the current day,
    ''' to calculate the remainder of days left in the current week
    i_AddNumberOfDays = 7 - d_StartDate.DayOfWeek

    ''' add the remainder of days in this week to the total
    ''' number of days we have to add to the date
    i_AddNumberOfDays += i_NumberOfDays

    ''' divide by 5, because we need to know how many 
    ''' business weeks we are dealing with
    i_AddNumberOfDays = i_AddNumberOfDays / 5

    ''' multiply the integer of current business weeks by 2
    ''' those are the amount of days in the weekends we have 
    ''' to add to the total
    i_AddNumberOfDays = Int(i_AddNumberOfDays) * 2

    ''' add the number of days to the weekend days
    i_AddNumberOfDays += i_NumberOfDays

Else

    ''' there are enough businessdays left in this week
    ''' to add the given amount of days
    i_AddNumberOfDays = i_NumberOfDays

End If

''' this is the numberof dates to calculate with in DateAdd
dim d_CalculatedDate as Date
d_CalculatedDate = DateAdd(DateInterval.Day, i_AddNumberOfDays, d_StartDate)

预先感谢您对此的评论和意见。

最佳答案

我使用 .DayOfWeek 函数来检查这是否是周末。这不包括假期实现。它已经过测试。我意识到这个问题很旧,但接受的答案不起作用。然而,我确实喜欢它的干净程度,所以我想我会更新它并发布。我确实改变了 while 循环中的逻辑。

Function AddBusinessDays(startDate As Date, numberOfDays As Integer) As Date
    Dim newDate As Date = startDate
    While numberOfDays > 0
        newDate = newDate.AddDays(1)

        If newDate.DayOfWeek() > 0 AndAlso newDate.DayOfWeek() < 6 Then '1-5 is Mon-Fri
            numberOfDays -= 1
        End If

    End While
    Return newDate
End Function

关于vb.net - 计算日期添加,但仅限工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1569467/

相关文章:

VB.NET If(三元)错误

c# - .NET 框架中的 lambda 是如何解析的?

ios - 找出最后触发的 UILocalNotification

R:如何根据属性值(日期)逐行进行分组排序?

c# - 我可以将枚举添加到现有的 .NET 结构中,例如日期吗?

vb.net - 当 T 是值类型时,从字符串转换为泛型类型 T 的更快方法?

sql-server - 如何保留指示日期是周末的位字段?

java - DateFormat.parse(String) 究竟如何处理非日期尾随文本?

mysql - 选择日期小于指定日期的每组最新行

vb.net - 在 vb.net 中的特定索引处启动循环?