Csharp Date functions

0 comments

private int GetAge(DateTime birthDate)
{
 // cache the current time
  DateTime now = DateTime.Today; // today is fine, don't need the timestamp from now
  // get the difference in years
  int years = now.Year - birthDate.Year;
  // subtract another year if we're before the
  // birth day in the current year
  if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
  {
      --years;
  }

  return years;

}


Comments


Leave a Comment