Introduction
This article explains how to calculate someone's age in C#.
C# Code-
DateTime bDay = new DateTime(2000, 2, 29); DateTime now = new DateTime(2009, 2, 28); MessageBox.Show(string.Format("Test {0} {1} {2}", CalculateAge(bDay, now))); // outputs 8
public int CalculateAge(DateTime birthDate, DateTime now)
{
int age = now.Year - birthDate.Year;
if (now.Month < birthDate.Month ||
(now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;
return age;
}
No comments:
Post a Comment