问题描述
我想根据以下条件在 ms 访问中获取周数:
https://www.kalender-365.nl/kalender-2021.html
我的表情:
test: DatePart("ww",#01/01/2021#,2,2)
这将返回 1
而不是周 53
。我该怎么做才能返回 53
?
语法:
DatePart(datepart,date,firstdayofweek,firstweekofyear)
2 = 星期一 (firstdayofweek)
2 = 使用一年中至少有 4 天的第一周 (firstdayofyear)
解决方法
您可能还需要匹配的 ISO 年份:
Public Const MaxWeekValue As Integer = 53
Public Const MinWeekValue As Integer = 1
Public Const MaxMonthValue As Integer = 12
Public Const MinMonthValue As Integer = 1
' Returns the ISO 8601 week of a date.
' The related ISO year is returned by ref.
'
' 2016-01-06. Gustav Brock,Cactus Data ApS,CPH.
'
Public Function Week( _
ByVal Date1 As Date,_
Optional ByRef IsoYear As Integer) _
As Integer
Dim Month As Integer
Dim Interval As String
Dim Result As Integer
Interval = "ww"
Month = VBA.Month(Date1)
' Initially,set the ISO year to the calendar year.
IsoYear = VBA.Year(Date1)
Result = DatePart(Interval,Date1,vbMonday,vbFirstFourDays)
If Result = MaxWeekValue Then
If DatePart(Interval,DateAdd(Interval,1,Date1),vbFirstFourDays) = MinWeekValue Then
' OK. The next week is the first week of the following year.
Else
' This is really the first week of the next ISO year.
' Correct for DatePart bug.
Result = MinWeekValue
End If
End If
' Adjust year where week number belongs to next or previous year.
If Month = MinMonthValue Then
If Result >= MaxWeekValue - 1 Then
' This is an early date of January belonging to the last week of the previous ISO year.
IsoYear = IsoYear - 1
End If
ElseIf Month = MaxMonthValue Then
If Result = MinWeekValue Then
' This is a late date of December belonging to the first week of the next ISO year.
IsoYear = IsoYear + 1
End If
End If
' IsoYear is returned by reference.
Week = Result
End Function