Skip to content

Instantly share code, notes, and snippets.

@sachintha81
Created October 10, 2017 21:34
Show Gist options
  • Save sachintha81/7a452c2d1ef5aebeacdb1aca69b414cc to your computer and use it in GitHub Desktop.
Save sachintha81/7a452c2d1ef5aebeacdb1aca69b414cc to your computer and use it in GitHub Desktop.
public static void CalculateAge(DateTime dt1, DateTime dt2, out int years, out int months, out int days)
{
DateTime start = dt1 < dt2 ? dt1 : dt2;
DateTime end = dt2 > dt1 ? dt2 : dt1;
years = 0;
months = 0;
days = 0;
while (start.AddYears(1) < end)
{
start = start.AddYears(1);
years++;
}
while (start.AddMonths(1) < end)
{
start = start.AddMonths(1);
months++;
}
while (start.AddDays(1) < end)
{
start = start.AddDays(1);
days++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment