如何查找两个日期范围内的所有星期一

How to find all Mondays in two date ranges

我正在尝试获取使用输入的日期范围内所有星期一日期的列表。我已经完成了大部分工作,只是无法弄清楚如何在开始日期和结束日期之间添加星期一并将它们放入列表框中的 for next 循环。

这不是家庭作业,只是我和一个朋友一起做的,试图教他更多关于 VB 的知识,因为我也是新手。

这是我目前拥有的。

Private Sub btnCountMondays_Click(sender As Object, e As EventArgs) Handles btnCountMondays.Click
    Dim startDate As Date
    Dim endDate As Date
    startDate = txtStartDate.Text
    endDate = txtEndDate.Text
    txtDaysBetween.Text = DateDiff("d", startDate, endDate)

    For monday As Integer = 0 To Val(txtDaysBetween.Text)
        If monday = 1 Then
            lstResultsBox.Items.Add()
        End If
    Next
End Sub

如果你必须使用for循环来完成,你可以按如下方式完成

Private Sub btnCountMondays_Click(sender As Object, e As EventArgs) Handles btnCountMondays.Click
    Dim startDate As Date
    Dim endDate As Date
    startDate = CDate(txtStartDate.Text)
    endDate = CDate(txtEndDate.Text)
    Dim nDays As Long = DateDiff("d", startDate, endDate) + 1
    For i As Long = 1 To nDays
        If startDate.DayOfWeek = DayOfWeek.Monday Then
            lstResultsBox.Items.Add(startDate.ToString("dd-MM-yyyy"))
        End If
        startDate = startDate.AddDays(1)
    Next
End Sub

获取 2 个日期之间所有星期一的日期列表。获得愤怒 dates.and 在循环期间获得星期几

Dim dt1 As New DateTime(2012, 5, 1)
Dim dt2 As DateTime = DateTime.Now
Dim ddl As New List(Of String)()
While dt1 < dt2
    Dim dow As DayOfWeek = dt1.DayOfWeek
    'enum
    Dim str As String = dow.ToString()
    'string
    If str.ToLower() = "monday" Then

        ddl.Add(dt1.ToString("dd-MMM-yyyy"))
    End If


    dt1 = dt1.AddDays(1)
End While