Pages

Wednesday, August 7, 2013

How to Calculate Age in Asp.net

This article will be explain how to create Age calculator in Asp.net using c#. To calculate age using following code you have to enter your date of birth in textbox in dd//mm//yyyy format.
Here is Default.aspx page code
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
        <asp:TextBox ID="txtMonth" runat="server" style="top: 249px; left: 412px; position: absolute; height: 22px; width: 200px"></asp:TextBox>
        <asp:TextBox ID="txtYear" runat="server" style="top: 298px; left: 411px; position: absolute; height: 22px; width: 200px"></asp:TextBox>
        <p>
            <asp:TextBox ID="txtDay" runat="server" style="top: 200px; left: 413px; position: absolute; height: 22px; width: 200px"></asp:TextBox>
        </p>
        <asp:Button ID="FindAge" runat="server" style="top: 345px; left: 544px; position: absolute; height: 26px; width: 80px" Text="Find Age" OnClick="FindAge_Click" />
        <asp:Label ID="Result" runat="server" style="top: 428px; left: 387px; position: absolute; height: 19px; width: 321px" ForeColor="Red"></asp:Label>
        <asp:Label ID="Label1" runat="server" style="top: 207px; left: 346px; position: absolute; height: 21px; width: 60px" Text="Day"></asp:Label>
      
        <asp:Label ID="Label2" runat="server" style="top: 256px; left: 346px; position: absolute; height: 19px; width: 55px" Text="Month"></asp:Label>
        <asp:Label ID="Label3" runat="server" style="top: 306px; left: 349px; position: absolute; width: 49px" Text="Year"></asp:Label>
        <asp:Label ID="header" runat="server" style="top: 139px; left: 413px; position: absolute; height: 19px; width: 186px" Text="Enter your Dob"></asp:Label>
      
    </form>
</body>
Here is Default.aspx.cs page code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void FindAge_Click(object sender, EventArgs e)
    {
        int year, month, day;
        year = Convert.ToInt32(txtYear.Text);
        month = Convert.ToInt32(txtMonth.Text);
        day = Convert.ToInt32(txtDay.Text);
        DateTime dateTime = new DateTime(year, month, day);
        DateTime td = DateTime.Now;
        int leapYear = 0;
        for (int i = dateTime.Year; i < td.Year; i++)
        {
            if (DateTime.IsLeapYear(i))
            {
                ++leapYear;
            }
        }
        TimeSpan timespan = td.Subtract(dateTime);
        day = timespan.Days - leapYear;
        int intResult = 0;
        year = Math.DivRem(day, 365, out intResult);
        month = Math.DivRem(intResult, 30, out intResult);
        day = intResult;
        Result.Text = "Your Age is" + year.ToString() + " Year/s " + month + " Month/s " + day + " Day/s";
    }

}

No comments:

Post a Comment