Calculate Age From Date of Birth ASP.Net VB

misnstt
misnstt used Ask the Experts™
on
I have a ASP.Net Page which contains a date of birth Text Box and a Age Text Box.  I want that when the user enters the Date of Birth the  age is automaticaly imputed in the Age Text Box.  Using either script or of necessary a command button.   Im using VB with Ajax extentions.  
Thanks for any help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:


   1. Dim now As DateTime = DateTime.Now
   2. Dim age As Integer = now.Year - bday.Year
   3. If now < bday.AddYears(age) Then
   4.     age -= 1
   5. End If

Author

Commented:
Hello I am a beginner.  Do I add this to the On click of a Button?  

Commented:
you're using VB.NET?


<asp:Text id=txtAge runat="server>



<asp:Button id="btnComputerAge runat="server Text="Calculate" />





in code:  

Private Sub btnComputerAge_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles btnComputerAge.Click


    Dim now As DateTime = DateTime.Now
    DateTime bday = Convert.ToDateTime(txtBDay.Text)
    Dim age As Integer = now.Year - bday.Year
    If now < bday.AddYears(age) Then
        age -= 1
    End If
   
   txtAge.Text = age.ToString()


End Sub


Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Commented:
asp tag portion...this is like adding <input ...except you using aspx tags

Enter BirthDay: <asp:Text id="bday" runat="server />


Age is: <asp:Text id=txtAge runat="server />


<asp:Button id="btnComputerAge runat="server Text="Calculate" />


//in page...if you don't declare script language, it will choose default ASPX language...i.e. if you're using C# then it //would be c#...if your page uses c#, your script must be in C#...can't Use VB...and vice versa..
<script>
Private Sub btnComputerAge_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles btnComputerAge.Click


    Dim now As DateTime = DateTime.Now
    DateTime bday = Convert.ToDateTime(txtBDay.Text)
    Dim age As Integer = now.Year - bday.Year
    If now < bday.AddYears(age) Then
        age -= 1
    End If
   
   txtAge.Text = age.ToString()


End Sub
</script>

Commented:
or  you could copy and paste the code that's in the script tag and write that method in your code-behind page...either way will work

Author

Commented:
Hello I am getting the following error message.
Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
 
Compiler Error Message: BC30108: 'DateTime' is a type and cannot be used as an expression.
 
Source Error:
 
 
 
Line 107:    Private Sub btnComputerAge_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComputerAge.Click
Line 108:        Dim now As DateTime = DateTime.Now
Line 109:        DateTime(bday = Convert.ToDateTime(txtBDay.Text))
Line 110:        Dim age As Integer = now.Year - bday.Year
Line 111:        If now < bday.AddYears(age) Then
 

Open in new window

Commented:
Change this line:
DateTime(bday = Convert.ToDateTime(txtBDay.Text))

To:
Dim bday DateTime = Convert.ToDateTime(txtBDay.Text))
Replace this line

 DateTime(bday = Convert.ToDateTime(txtBDay.Text))

with

Dim bday as DateTime=Convert.ToDateTime(txtBDay.Text)

Commented:
oops...excuse the typo...

Commented:
oh...i didn't have a typo...more than likely VB wrapped that line up when you copied and pasted...for some reason, it does that sometimes...

Commented:
here is the code:
<head>
<script>
function getAge(birthDay){
        now = new Date()
        yearsTillNow = Math.floor((now.getTime() - birthDay.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
        return yearsTillNow;
} 
function getBirthdate()
{
        var bd = document.getElementById("txtBD").value;
        
		var arrBD = bd.split("/"); 
        
        var age = getAge(new Date(arrBD[2], arrBD[0], arrBD[1])); 
        var txtAge = document.getElementById("txtAge");
        txtAge.value = age;
} 
 
</script>
</head>
<body>
<form>
mm/dd/yyyy <input id="txtBD" type=text value="">
<input id="txtAge" type=text value="">
<input type="button" value="clickme" onclick="getBirthdate();">
</form>
</body>
</html>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial