How to Calculate Age in Year in ASP.NET

Here are two functions that return age from a date of birth in ASP.NET and C#.

Here are two methods in C# that calculate age from a DOB.

Function1 
  1. public int get_age(DateTime dob)  
  2. {  
  3. int age = 0;  
  4. age = DateTime.Now.Subtract(dob).Days;  
  5. age = age / 365;  
  6. return age;  
  7. }  
Function 2 
  1. public int get_age2(DateTime dob)  
  2. {  
  3. int age = 0;  
  4. age = DateTime.Now.AddYears(-dob.Year).Year;  
  5. return age;  
  6. }  
Now call these functions. 
  1. Response.Write(get_age(Convert.ToDateTime("6/20/1993")));  
  2. Response.Write(get_age2(Convert.ToDateTime("6/20/1993")));