protected int getAge()
{
TimeSpan t = Convert.ToDateTime(DateTime.Now) - Convert.ToDateTime(("10/19/1977"));
int leapdays = getLeapDays(Convert.ToDateTime(("10/19/1977")), Convert.ToDateTime(DateTime.Now));
int Totaldays = Convert.ToInt32(t.TotalDays - leapdays);
int yr = Totaldays / 365;
if (yr <= 0)
yr = 1;
return yr;
}
private int getLeapDays(DateTime startDate, DateTime endDate)
{
int leapdays = 0;
while (startDate < endDate)
{
if ((Convert.ToInt32(startDate.Year) % 4) == 0)
leapdays++;
startDate = startDate.AddYears(1);
}
return leapdays;
}
Page Load:
int age = getAge();
Response.Write(age);
To get exact age in years, month and day refer this link
ReplyDeletehttp://itdeveloperzone.blogspot.in/2012/02/difference-in-years-months-and-days.html