3
Contributors
5
Replies
34
Views
4 Years
Discussion Span
Last Post by Reverend Jim
0

As long as you have verified that the value entered is a valid date and is less than or equal to the current date then convert that value to a date and calculate the difference in years between that date and now. Then you have to subtract one if the birthday falls later than the current date.

Dim birth As Date = CDate(MaskedTextBox1.Text)
Dim age As Integer = DateDiff(DateInterval.Year, birth, Now)

If birth.AddYears(age) > Now Then
    age -= 1
End If
0

You could tie that to the Textchanged event or the validation event to have the calculation done automatically.

0

You could tie that to the Textchanged event

You wouldn't want to do that because it fires on every keystroke and you don't want to do the calculation until the entire date has been entered.

0

You wouldn't want to do that because it fires on every keystroke and you don't want to do the calculation until the entire date has been entered.

Unless you also wanted on the fly validation before the calculation, then do the calculation on a specific keystroke.

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.