Calculate DOB in Years,Month and Days

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 349
Write a static function like below and call it inside any function:-

public static string DateDiff(DateTime dt1, DateTime dt2)
{
TimeSpan t1 = dt2 - dt1;
Decimal d1 = Convert.ToDecimal(t1.TotalDays) / 365.25M;
Decimal d2 = Decimal.Floor(d1);
Decimal m1 = (d1 - d2) * 12;
Decimal m2 = Decimal.Floor(m1);
Decimal day1 = (m1 - m2) * 24;
Decimal day2 = Decimal.Floor(day1);

return String.Format("{0} Years {1} Months {2} Days ", d2, m2, day2);
}


Now,Call it like below:-
DateTime dt1 = new DateTime(1984,10,15);
DateTime dt2 = DateTime.Today;

Response.Write(DateDiff(dt1, dt2));

Comments or Responses

Login to post response