Click here to Skip to main content
15,896,360 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
My vb code here.please help me
C#
Private Function CalculateAge(ByVal Dob As DateTime) As String
       Dim Now As DateTime = DateTime.Now
       Dim Years As Integer = New DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1
       Dim PastYearDate As DateTime = Dob.AddYears(Years)
       Dim Months As Integer = 0
       For i As Integer = 1 To 12
           If PastYearDate.AddMonths(i) = Now Then
               Months = i
               Exit For
           ElseIf PastYearDate.AddMonths(i) >= Now Then
               Months = i - 1
               Exit For
           End If
       Next
       Dim Days As Integer = Now.Subtract(PastYearDate.AddMonths(Months)).Days
       Dim Hours As Integer = Now.Subtract(PastYearDate).Hours
       Dim Minutes As Integer = Now.Subtract(PastYearDate).Minutes
       Dim Seconds As Integer = Now.Subtract(PastYearDate).Seconds
       Return [String].Format("{0} Years", Years)
   End Function


   Protected Sub btnGetAge_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetAge.Click
       If (CType(Session.Item("uno"), String) = "") Or (CType(Session.Item("urights"), String) = "") Then
           Server.Transfer("~/default.aspx", True)
       Else
           Dim dtVal As String = Trim(TxtDob.Text)
           Dim Dob As DateTime = Convert.ToDateTime(dtVal)
           'DateTime dob = DateTime.ParseExact(d, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
           txtage.Text = CalculateAge(Dob)
       End If
   End Sub
Posted
Updated 3-Dec-13 18:13pm
v2
Comments
sahabiswarup 3-Dec-13 23:52pm    
why you are using trim function?? just remove the white space only?? why don't you put calender extender instead of simple textbox. Then no need to use this trim, and that may solve your problem.
Thanks7872 4-Dec-13 0:17am    
Remove unnecessary code. Only include the code with which you got this error. Its not clear from the above code.
Aravindba 4-Dec-13 1:35am    
hai ,if i run this code ,i will get correct age,it not show any error,try in new page above codings only

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900