calculate how many weekend days inside date?

别等时光非礼了梦想. 提交于 2019-12-12 01:08:21

问题


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


来源:https://stackoverflow.com/questions/16786735/calculate-how-many-weekend-days-inside-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!