计算两个日期间的工作日

2008-02-23 06:56:11来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

本示例的功能是如何计算给定的两个日期之间的工作日,你可以选择是否将星期六或星期日也包括进去。同时本程序还允许你可入一些假期,从而在计算时将这些假期排除在外。

函数:

Public Function BusinessDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Long

该函数的功能是计算两个日期(dDate1和dDate2)之间的工作日。下面的这些属性允许你对计算实施控制,告诉程序如何进行计算。

属性

Public IncludeSaturdays As Boolean

当值为真,计算时将包括星期六,否则将星期六排除在外。

Public IncludeSundays As Boolean

当值为真,计算时将包括星期日,否则将星期日排除在外。

Public Property Let IncludeFirstDate(bInc As Boolean) Public Property
Get IncludeFirstDate() As Boolean

在默认情况下,BusinessDateDiff函数会将你输入的起始日包括在计算中,但不包括你输入的终止日。将该属性值设为假,计算时就不包括你输入的起始日。

Public Property Let IncludeLastDate(bInc As Boolean)

Public Property Get IncludeLastDate() As Boolean

在默认情况下,BusinessDateDiff函数不包括你输入的最后一天。将该属性值设为真,程序把你输入的最后一天包括在计算中。

方法

Public Sub HolidayAdd(dHoliday As Date)

添加一个假期列表,以便在计算时排除这些假期。在默认情况下,没有添加任何假期。

Public Sub HolidayRemove(dHoliday As Date)

从假期列表移除假期

Public Sub HolidayClear()

清除全部假期列表

代码清单

首先建立一个新的类模块,名为cBusinessDates,将下面的代码粘贴进去。

Option Explicit

Public IncludeSaturdays As Boolean
Public IncludeSundays As Boolean

Private mbIncludeFirstDate As Boolean
Private mbIncludeLastDate As Boolean

Private Holidays As New Collection

Public Sub HolidayAdd(dHoliday As Date)

On Error Resume Next

Holidays.Add dHoliday, "D" & dHoliday

If Err Then
Err.Clear
End If

End Sub

Public Sub HolidayRemove(dHoliday As Date)

On Error Resume Next

Holidays.Remove "D" & dHoliday

If Err Then
Err.Clear
End If

End Sub

Public Sub HolidayClear()

Dim x As Long

For x = 1 To Holidays.Count
Holidays.Remove 1
Next

End Sub

Public Property Let IncludeFirstDate(bInc As Boolean)

mbIncludeFirstDate = bInc

End Property

Public Property Let IncludeLastDate(bInc As Boolean)

mbIncludeLastDate = bInc

End Property

Public Property Get IncludeFirstDate() As Boolean

IncludeFirstDate = mbIncludeFirstDate

End Property

Public Property Get IncludeLastDate() As Boolean

IncludeLastDate = mbIncludeLastDate

End Property

'该函数不把dDate2包含在计算中,如果dDate2是星期日,而你又选择了计算星期日,该日期仍不计算在内。

Public Function BusinessDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Long

Dim dCurDate As Date
Dim dLastDate As Date
Dim dFirstDate As Date
Dim lDayCount As Long
Dim eDay As VBDayOfWeek
Dim dHoliday As Variant

'调整应将哪一个日期包括在计算中(第一个还是第二个)

If IncludeFirstDate Then
dFirstDate = dDate1
Else
dFirstDate = dDate1 1
End If

If IncludeLastDate Then
dLastDate = dDate2 1
Else
dLastDate = dDate2
End If

'在所有日期中循环,并更新日期计数

dCurDate = dFirstDate

Do While dCurDate <> dLastDate

eDay = WeekDay(dCurDate)

If IncludeSaturdays And eDay = vbSaturday Then
lDayCount = lDayCount 1
End If

If IncludeSundays And eDay = vbSunday Then
lDayCount = lDayCount 1
End If

If eDay >= vbMonday And eDay <= vbFriday Then
lDayCount = lDayCount 1
End If

dCurDate = dCurDate 1

Loop

'根据假期调整日期计数

For Each dHoliday In Holidays

'如果假期在你所输入的日期范围之内

If CDate(dHoliday) >= dFirstDate And CDate(dHoliday) <= dLastDate Then

eDay = WeekDay(CDate(dHoliday))

If IncludeSaturdays And eDay = vbSaturday Then
lDayCount = lDayCount - 1
End If

If IncludeSundays And eDay = vbSunday Then
lDayCount = lDayCount - 1
End If

If eDay >= vbMonday And eDay <= vbFriday Then
lDayCount = lDayCount - 1
End If

End If

Next

BusinessDateDiff = lDayCount

End Function

Private Sub Class_Initialize()

IncludeFirstDate = True
IncludeLastDate = False
IncludeSundays = False
IncludeSaturdays = False

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:设计简单的屏幕保护程序

下一篇:字符串中包含双引号