How to create a list of days starting with today and going back the last 30 days in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Last30Days
    Public Shared Function GetLast30Days() As List(Of Integer)
        Dim days As List(Of Integer) = New List(Of Integer)()
        Dim today As DateTime = DateTime.Now

        For i As Integer = 0 To 30 - 1
            Dim pastDate As DateTime = today.AddDays(-i)
            days.Add(pastDate.Day)
        Next

        Return days
    End Function

	Public Shared Sub Main(ByVal args As String())
        Dim days As List(Of Integer) = GetLast30Days()
        Console.Write("Days: [")

        For i As Integer = 0 To days.Count - 1
            Console.Write(days(i))
            If i < days.Count - 1 Then
                Console.Write(", ")
            End If
        Next

        Console.WriteLine("]")
    End Sub
End Class


' run:
'
' Days: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12]
'

 



answered Apr 10 by avibootz
...