## 6 replies

Last post Dec 22, 2008 12:07 PM by Mr^B

Member

80 Points

656 Posts

### Age Calculation in ASP.Net

Dear All,

I have to calculate age like this Year.Month. For this i made one calendar and one textbox for age.

On the click of calendar it should give me age in textbox.e.g  Calendare Value 12/22/2007 then text box should show 01.00

Participant

1699 Points

504 Posts

### Re: Age Calculation in ASP.Net

So you want to find the difference of time (timepsan) between the date of birth and today, that would be as easy as

Dim MyAgeValue as TimeSpan

MyAgeValue  = Now() - Calendar1.SelectedDate

Then you can get years, months, days and enything of that sort from this MyAgeValue

Thanks and Best Regards

S.K.Vignesh

Member

403 Points

100 Posts

### Re: Age Calculation in ASP.Net

try this this will give you the number of ays

TimeSpan t = new TimeSpan();
t = DateTime.Now.Subtract(Calendar1.SelectedDate); ;
TextBox1.Text = t.Days.ToString();

Regards,
Senthil Kumar.B
Blog : ginktage.com

Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread : )

All-Star

33715 Points

8365 Posts

MVP

### Re: Age Calculation in ASP.Net

http://forums.asp.net/t/396599.aspx

"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD

Member

403 Points

100 Posts

### Re: Age Calculation in ASP.Net

This will work,

TimeSpan t = new TimeSpan();
t = DateTime.Now - Calendar1.SelectedDate;
int v = t.Days;
int Count=0;
while ( v >=365)
{
Count++;
v -= 365;

}
TextBox1.Text = Count.ToString();

Regards,
Senthil Kumar.B
Blog : ginktage.com

Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread : )

Star

8340 Points

2237 Posts

### Re: Age Calculation in ASP.Net

#### imran_ku07

http://forums.asp.net/t/396599.aspx

That's for SQL Server, not for a web app.

In C# the easiest way is probably this:

int age = (int)((double)new TimeSpan(DateTime.Now.Subtract(dob).Ticks).Days / 365.25);

MCSD.Net

Star

8340 Points

2237 Posts

### Re: Age Calculation in ASP.Net

#### senthil.christcollege@gmail.com

This will work,

TimeSpan t = new TimeSpan();
t = DateTime.Now - Calendar1.SelectedDate;
int v = t.Days;
int Count=0;
while ( v >=365)
{
Count++;
v -= 365;

}
TextBox1.Text = Count.ToString();

What about leap years? A year isn't always 365 days long, so if he was in his 70s it could be as much as 17 days out.

MCSD.Net