Using DateTime to check "minor" status

  • Thread starter Thread starter Michael C#
  • Start date Start date
M

Michael C#

Given a DateTime variable, what's the best way to check "minor" status
(i.e., less than age 18 years) based on today's date? I came up with this:

Dim minor As Boolean = true
'dtDOB is a DateTime variable set to Date of Birth
If (DateTime.Now.Subtract(dteDOB.Value.AddYears(18)).TotalDays >= 0) Then
minor = false
End If

This appears to work, but seems a little obfuscated. I was just wondering
if there was an easier way, and also to double-check my sanity and make sure
that my logic/implementation aren't screwy.

Thanks
 
Michael,
I normally define a variable (possible a readonly class field) that is the
"minor" date, then compare my date of birth to it.

Something like:

Dim minorDob As DateTime = DateTime.Today.AddYears(-18)
Debug.WriteLine(minorDob, "minor date")

For Each dob As DateTime In New DateTime() {#3/18/1987#,
#3/19/1987#, #3/20/1987#, #3/21/1987#, #3/22/1987#}
If minorDob.CompareTo(dob) < 0 Then
Debug.WriteLine(dob, "minor")
Else
Debug.WriteLine(dob, "adult")
End If
Next

Hope this helps
Jay
 
Michael,

I find it nice code, when this is not to precise for you, than it will work
in my opinion as well.

\\\
If dteDOB.Ticks > Now.AddYears(-18).Ticks Then
minor = True
End If
///

I hope this helps,

Cor
 
Back
Top