How to Calculate Years and Months based on Enter Months in Asp.net



In this article I have explained how to get or compute years and months from the month’s value entered in textbox in Asp.Net.
Step 1: Create Webpage and Design it Like Below.
<div>
        <asp:TextBox ID="txtmonth" placeholder="Enter Number of Months" runat="server"></asp:TextBox>
        <asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" /><br />
        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </div>

Step 2: Now Code to calculate for Number of Years and Months Based on Entered Month in Textbox.
protected void btnCalculate_Click(object sender, EventArgs e)
        {
            ltrlExperince.Text = Calculate(Convert.ToInt32(txtmonth.Text.Trim()));
        }

public string Calculate(Int32 months)
        {
            int numberOfYears, numberOfMonths;
            numberOfYears = Math.DivRem(months, 12, out numberOfMonths);
            return string.Format("{0} {1}", numberOfYears + " Year", numberOfMonths > 1 ? numberOfMonths + " Months" : numberOfMonths + " Month");
        }

Step 3: Now run your Webpage and Enter Number of Months in Textbox and Click Calculate Button to Count it.


Related Posts

Previous
Next Post »

Thanks for comments.....