是否有人在vb6中返回给定日期的周数的工作函数?
这曾经有用:
Dim W As Integer
W = Format(DateSerial(2010, 1, 1), "ww", vbMonday, vbFirstFourDays)
但在Windows 8.1中,您现在可以获得“超出堆栈空间”。
答案 0 :(得分:1)
给定偏移
,答案有点复杂以下功能应该可以满足您的需求。只需传递一个有效的Date对象,它就会返回一个周数的整数。
Private Function Week(dteValue As Date) As Integer
'Monday is set as first day of week
Dim lngDate As Long
Dim intWeek As Integer
'If january 1. is later then thursday, january 1. is not in week 1
If Not Weekday("01/01/" & Year(dteValue), vbMonday) > 4 Then
intWeek = 1
Else
intWeek = 0
End If
'Sets long-value for january 1.
lngDate = CLng(CDate("01/01/" & Year(dteValue)))
'Finds the first monday of year
lngDate = lngDate + (8 - Weekday("01/01/" & Year(dteValue), vbMonday))
'Increases week by week until set date is passed
While Not lngDate > CLng(CDate(dteValue))
intWeek = intWeek + 1
lngDate = lngDate + 7
Wend
'If the date set is not in week 1, this finds latest week previous year
If intWeek = 0 Then
intWeek = Week("31/12/" & Year(dteValue) - 1)
End If
Week = intWeek
End Function
此代码由FreeVBCode.com提供(包含对原作者提供属性的引用)。