Calculate someone's age in C#

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; 

}

 

Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment