calculate how many weekend days inside date?

2019-09-02 13:05发布

问题:

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.

回答1:

The following VBA function will allow you to run Access queries of the form

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

Just create a new Module in Access and paste the following code into it:

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