Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compute age from given birthdate

I have 2 comboboxes and 2 textboxes. My first combobox contains months in this format january, february, etc, and the other combobox contains numbers from 1 to 31. My first textbox is txtyear. Once the user input birth year to txtyear a variable BOD will be equals to this.

Dim BOD as string
BOD = cbomonth.text + "-" + cboday.text + "-" + txtyear.text

The purpose of my last textbox is to handle the age of the user that will be computed when the cursor lost focus on txtyear.

Can anyone help how to compute the age.

like image 987
joshua Avatar asked Dec 12 '22 14:12

joshua


1 Answers

There are really two questions here:

  1. How to convert the string input into a DateTime object
  2. How to calculate age once you have your data in the correct format.

I'll let you follow other's instructions for how use TryParseExtract which is definitely the correct way to go here.

When determining someone's age from their DOB, try using this:

Public Function GetCurrentAge(ByVal dob As Date) As Integer
    Dim age As Integer
    age = Today.Year - dob.Year
    If (dob > Today.AddYears(-age)) Then age -= 1
    Return age
End Function

It is the vb version of the top answers on Jeff Atwood's very popular question How do I calculate someone's age

I wrote a blogpost about calculating age from dob as well.

like image 135
KyleMit Avatar answered Dec 14 '22 02:12

KyleMit