How to Calculate Number of Days in Month using ASP.NET
ASP.NET developers may need to calculate number of days in month for different reasons.
One reason might be to populate a calendar days dropdownlist.
If the ASP.NET developer knows that in February there is 29 days for that year, developer will code in VB.NET or in C# in order to populate day combobox with 29 items beginning from 1 to 29.
Another reasom may be to find the last day in month.
If the ASP.NET developer knows how many days are there for a specific month, he or she will be able to calculate last day of month easily.
Here is a web page from design view which is from a VB.NET sample application project.
The ASP.NET developers will realize that there are three DropDownList items with first two have AutoPostBack properties are equal to True.
The first dropdownlist is used to display year for the web user to select a date from.
The second dropdownlist is used for the web user to pick a month from the displayed list of items.
Here is the ASP.NET source code in HTML view.
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True">2011</asp:ListItem>
<asp:ListItem>2012</asp:ListItem>
<asp:ListItem>2013</asp:ListItem>
<asp:ListItem>2014</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="True">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlDay" runat="server">
</asp:DropDownList>
Up to now, there is not any problem.
But when it is to decide how many days exist for a specific month year combination, the ASP.NET programmer might need some help.
If you look at the PopulateDaysDDL sub procedure, you will realize that we are using DateTime.DaysInMonth public shared method in order to calculate number of days in the given year month combination.
If ASP.NET developer passes year and month values as input arguments to the public shared DateTime.DaysInMonth method, the return value will be an integer giving the number of days in month requested.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateDaysDDL()
End If
End Sub
Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlMonth.SelectedIndexChanged
PopulateDaysDDL()
End Sub
Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlYear.SelectedIndexChanged
PopulateDaysDDL()
End Sub
Private Sub PopulateDaysDDL()
ddlDay.Items.Clear()
Dim lv_numberofdays As Integer
lv_numberofdays = DateTime.DaysInMonth(ddlYear.SelectedValue, ddlMonth.SelectedValue)
Dim lv_lastdayofmonth As Date = New Date(ddlYear.SelectedValue, ddlMonth.SelectedValue, DateTime.DaysInMonth(ddlYear.SelectedValue, ddlMonth.SelectedValue))
Dim i As Integer = 1
While i <= lv_numberofdays
ddlDay.Items.Add(i.ToString)
i = i + 1
End While
End Sub
If you look in detail to the above VB.NET code, you will realize that the above code also answers how to calculate last day of month.
The lv_lastdayofmonth local date variable is used to find last day of month requested.
The last day in a month is calculated by using New Date(year, month, day) method.
Since we already know the last day of month, it is easy for VB.NET or CSharp developers to form the date variable using New Date() method passing the year, month and day arguments.