Calculating the person age using C#.
Code :
The following code describe that how to calculate the person age using C#.
using System;
public class Program
{
public static void Main()
{
DateTime dob = Convert.ToDateTime("26 Aug 1989");
DateTime PresentYear = DateTime.Now;
TimeSpan ts = PresentYear - dob;
DateTime Age = DateTime.MinValue.AddDays(ts.Days);
Console.WriteLine(string.Format(" {0} Years {1} Month {2} Days", Age.Year - 1, Age.Month - 1, Age.Day - 1));
Console.WriteLine(string.Format(" {0} Years {1} Month", Age.Year - 1, Age.Month - 1));
Console.WriteLine(string.Format(" {0} Years", Age.Year - 1));
}
}
Output : 26 Years 8 Month 16 Days
26 Years 8 Month
26 Years
26 Years 8 Month
26 Years
Demo : https://dotnetfiddle.net/QNIiHJ
