ARTICLE

Find Days in a Month using DateTime

Posted by John Arcadella Articles | Visual Basic 2010 January 26, 2011
How to find days in a month using DateTime in VB.NET.
 
Reader Level:

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

 

Listing 7

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor