Find Days in a Month using DateTime
How to find days in a month using DateTime in VB.NET.
Find Days in a Month
The DaysInMonth static method returns number of days in a month. This method takes a year and a month in numbers from 1 to 12. The code snippet in Listing 6 gets number of days in Feb month of year 2002.The output is 28 days.
Private Sub GetDaysInAMonth()
Dim days As Int16 = DateTime.DaysInMonth(2002, 2)
Console.WriteLine(days)
End Sub
Listing 6
Using the same approach, we can find out total number of days in a year. The GetDaysInAYear method in Listing 7 takes a year and returns total number of days in that year.
Private Function GetDaysInAYear(ByVal year As Int16) As Int16
Dim days As Int16 = 0
For i As Integer = 1 To 12
days += DateTime.DaysInMonth(year, i)
Next
Return days
End Function