0

i was working with a simple table for calculating age of a user when he has entered in a text box and has to subtracted from the present time can you help me out.
Here is the Code i am Using to Calculate.

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{   
     DateTime now = DateTime.Now;
     DateTime givenDate = DateTime.Parse(DOB);
     int days = now.Subtract(givenDate).Days;
     int age = Math.Floor(days / 365.24219);
}
}

if is there any changes tell me i will change

coolprarun
  • 1,153
  • 2
  • 15
  • 22
HARI KRISHNA
  • 889
  • 1
  • 6
  • 8
  • possible duplicate of [How to calculate age in years from dob in c#](http://stackoverflow.com/questions/14766086/how-to-calculate-age-in-years-from-dob-in-c-sharp) – Shankar Narayana Damodaran Sep 02 '13 at 06:30
  • Can you Please refer these Question that are asked in Stackoverflow already. Think to it might Help You out [Calculate age](http://forums.asp.net/t/1798317.aspx/1) [http://stackoverflow.com/questions/10618207/how-would-you-calculate-the-age-in-c-sharp-using-date-of-birth-considering-leap](http://stackoverflow.com/questions/10618207/how-would-you-calculate-the-age-in-c-sharp-using-date-of-birth-considering-leap) [http://stackoverflow.com/questions/11717149/calculate-age-from-date-in-textbox-in-c-sharp](http://stackoverflow.com/questions/11717149/calculate-age-from-date-in-textbox-in-c-sharp) [h – coolprarun Sep 02 '13 at 06:29

1 Answers1

0

You can use TimeSpan to get the difference of the two dates

System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
            System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
            System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

            // diff1 gets 185 days, 14 hours, and 47 minutes.
            System.TimeSpan diff1 = date2.Subtract(date1);

            // date4 gets 4/9/1996 5:55:00 PM.
            System.DateTime date4 = date3.Subtract(diff1);

            // diff2 gets 55 days 4 hours and 20 minutes.
            System.TimeSpan diff2 = date2 - date3;

            // date5 gets 4/9/1996 5:55:00 PM.
            System.DateTime date5 = date1 - diff2;

Referenced from http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50