Home
Manage Your Code
Snippet: Age Calculator (C#)
Title: Age Calculator Language: C#
Description: HowOldAreYou() Views: 111
Author: Joshy George Date Added: 8/21/2008
Copy Code  
1	public string HowOldAreYou()
2		{
3			DateTime birthDayThisYear = new DateTime(DateTime.Today.Year,monthBirth,dayBirth);
4			int years = DateTime.Today.Year - yearBirth;
5			int months = DateTime.Today.Month - monthBirth;
6			int days = DateTime.Today.Day - dayBirth;
7			if(birthDayThisYear==DateTime.Today)
8			{
9				return "HAPPY BIRTHDAY TO YOU ....\n You just turned " + years.ToString() + " years old";
10			}
11			if(birthDayThisYear > DateTime.Today)//you've not yet celebrated your birthday this year
12			{
13				years -= 1;
14				months += 12;
15			}
16			if(birthDayThisYear.Day > DateTime.Today.Day)
17			{
18				months -= 1;
19				DateTime dt = new DateTime(birthDayThisYear.Year,DateTime.Today.Month -1,birthDayThisYear.Day);
20				TimeSpan ts = DateTime.Today - dt;
21				days = ts.Days;
22			}
23			return "You have " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";
24		}