计算内部日期的周末天数?

时间:2013-05-28 07:55:23

标签: ms-access ms-access-2007 access-vba ms-access-2010

我需要计算2个日期内的周末天数?我的意思是我有2个约会,想知道星期六和星期五的数量。这些日期之间的星期日。

我在每条记录上都有2个日期(从日期到日期),并且想查询周末的数量。

1 个答案:

答案 0 :(得分: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