1
votes

How can I generate a list of dates based on the start and end date?

i) dt.Rows(k).Item("startdate") = start date = 2019-11-13

ii) dt.Rows(k).Item("enddate") = end date = 2019-11-27

 For p = dt.Rows(k).Item("startdate") To dt.Rows(k).Item("enddate")

         //display p value

  Next p
1

1 Answers

1
votes

If you get the number of days between those dates then you can iterate that many times + 1 (to include the start and end dates) and add the loop variable to the start date...

Module Module1

    Sub Main()
        Dim startDate = New DateTime(2019, 11, 13)
        Dim endDate = New DateTime(2019, 11, 27)

        Dim daysCount = (endDate - startDate).Days

        For i = 0 To daysCount
            Dim p = startDate.AddDays(i)
            Console.WriteLine(p.ToString("yyyy-MM-dd"))
        Next

        Console.ReadLine()

    End Sub

End Module

Outputs:

2019-11-13
2019-11-14
2019-11-15
2019-11-16
2019-11-17
2019-11-18
2019-11-19
2019-11-20
2019-11-21
2019-11-22
2019-11-23
2019-11-24
2019-11-25
2019-11-26
2019-11-27

Note how it is ... To daysCount rather than ... To daysCount - 1.


There is no point to making it more fiddly with LINQ for this example, but just in case it makes sense in some other circumstance:

For Each p In Enumerable.Range(0, (endDate - startDate).Days + 1).Select(Function(i) startDate.AddDays(i))
    Console.WriteLine(p.ToString("yyyy-MM-dd"))
Next