0

I need to calculate how many weekend days inside 2 dates? what I mean is that I have 2 dates and want to know the count of Saturdays & Sundays between these dates.

I have the 2 dates on each record (from date - to date) and want to query the count of weekends.

4

1 回答 1

2

以下 VBA 函数将允许您运行表单的 Access 查询

SELECT CountWeekendDays([from date], [to date]) AS WeekendDays FROM YourTable

只需在 Access 中新建一个Module并将以下代码粘贴到其中:

Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
Dim StartDate As Date, EndDate As Date, _
        WeekendDays As Long, i As Long
If Date1 > Date2 Then
    StartDate = Date2
    EndDate = Date1
Else
    StartDate = Date1
    EndDate = Date2
End If
WeekendDays = 0
For i = 0 To DateDiff("d", StartDate, EndDate)
    Select Case Weekday(DateAdd("d", i, StartDate))
        Case 1, 7
            WeekendDays = WeekendDays + 1
    End Select
Next
CountWeekendDays = WeekendDays
End Function
于 2013-05-28T09:01:19.640 回答