创建一个返回星期日日期的函数

时间:2013-09-16 06:44:10

标签: date vb6

大家好我在使用vb6我想创建一个仅返回星期日日期的函数,但我将通过下面的日期范围示例。

fnWorkSunday(#09/02/2013#, #09/16/2013#)

结果必须为:#09/08/2013##09/15/2013#

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

首先,我会创建一个数组来保存返回日期。

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

然后返回dataArray。