Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to subtract the date of birth which is is in the dobtxbx textbox as datetimepicker value so that i get the age in the agetxbx textbox.
agetxbx.Text = Date.Now.Year - dobtxbx.text
Posted

You cannot subtract a string from a DateTime.
Try:
VB
Dim dob As Datetime = DateTime.Parse(dobtxbx.Text)
Dim delta As TimeSpan = DateTime.Today - dob
Dim age As Double = Math.Floor(delta.TotalDays / 365.25)
agetxbx.Text = age.ToString()


Hope this helps.

[Edit] Corrected the code to compute the number of years from the number of days.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 9-Dec-15 10:27am    
Sure, a 5.
—SA
Masta Pee 9-Dec-15 10:28am    
when i try it, there is an error that say years is not a member of timespan.. options given are days and hours
phil.o 9-Dec-15 10:41am    
You're right; please see my updated answer
Masta Pee 10-Dec-15 4:15am    
it worked perfectly.. i also editted it to do other great stuffs... thank you once again
phil.o 10-Dec-15 4:25am    
You're very welcome :)
I would suggest reading .NET Book Zero, by Charles Petzold[^]. It explains why this will not work, and what you need to do. It is a great primer for .NET and C#.
 
Share this answer
 
Comments
Masta Pee 9-Dec-15 10:29am    
thank you but can you please give me a fair sample of how it should look like
Richard MacCutchan 9-Dec-15 12:12pm    
If you read that book you will find one, and many other useful samples also.
Masta Pee 10-Dec-15 4:14am    
thank you
 
Share this answer
 
Comments
Masta Pee 9-Dec-15 10:29am    
thank you but any example please
Michael_Davies 9-Dec-15 11:03am    
Yes there are on the link...
Masta Pee 10-Dec-15 4:14am    
thsnk you

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