大家好,我正在使用 vb6,我想创建一个只返回星期天日期的函数,但我将在下面传递一个日期范围的示例。
fnWorkSunday(#09/02/2013#, #09/16/2013#)
结果必须是:#09/08/2013#
和#09/15/2013#
尝试这个:
Sub listSundaysBetween(startDate As Date, endDate As Date)
Dim nextSunday As Date
nextSunday = startDate - Weekday(startDate) + 1
If nextSunday < startDate Then nextSunday = nextSunday + 7
While nextSunday < endDate
Debug.Print nextSunday
nextSunday = nextSunday + 7
Wend
End Sub
首先,我将创建一个数组来保存返回日期。
Dim dateArray() as Date
然后遍历您的日期范围:
While startDate <= endDate
If Weekday(startDate) = 1 Then
'or If WeekdayName(startDate) = "Sunday" Then
'1 = vbSunday, 2 = vbMonday...
ReDim Preserve dateArray(UBound(dateArray) + 1)
'Resize the array to accommodate the new date
'don't forget to preserve or all the data in the array will be lost
dateArray(UBound(dateArray)) = startDate
End If
DateAdd("d",1,startDate) 'increment the startDate
WEnd
然后返回数据数组。