如何构建MS Access查询以返回当前日期后的第二个周六日期?
计算当前日期后第二个周六的实现方案
Excel公式方案
使用WEEKDAY函数以周一为一周起始(参数2),确保计算逻辑统一,公式如下:
=TODAY() + IF(WEEKDAY(TODAY(), 2) < 6, 13 - WEEKDAY(TODAY(), 2), 20 - WEEKDAY(TODAY(), 2))
验证示例
- 当今日为2023年2月3日(周五,周数
5):13-5=8,2023/2/3 +8天=2023/2/11,符合需求。 - 当今日为周六(周数
6):20-6=14,加14天得到两周后的周六。 - 当今日为周日(周数
7):20-7=13,加13天得到两周后的周六。
Access实现方案
查询表达式
直接在查询中添加计算字段,以周一为一周起始:
DateAdd("d", IIf(Weekday(Date(), 2) < 6, 13 - Weekday(Date(), 2), 20 - Weekday(Date(), 2)), Date() ) AS SecondNextSaturday
VBA函数
如果需要复用计算逻辑,可编写自定义函数:
Function GetSecondNextSaturday() As Date Dim currentWeekday As Integer currentWeekday = Weekday(Date, vbMonday) ' 周一为一周第1天,周六=6,周日=7 Dim addDays As Integer If currentWeekday < 6 Then addDays = 13 - currentWeekday Else addDays = 20 - currentWeekday End If GetSecondNextSaturday = DateAdd("d", addDays, Date) End Function
调用时直接使用GetSecondNextSaturday()即可返回目标日期。
内容的提问来源于stack exchange,提问作者Matt Gilbert
相关产品推荐
相关产品推荐

