Limiting date input?

ABOU

Member
Joined
Apr 29, 2008
Messages
14
Programming Experience
Beginner
On my Client input form i want to limit the minimum age to 18 using dates as opposed to numbers.

I have been thinking on how to go about this, i thought that a datecompare would be the way to go, but i have no idea how to compare 2 dates that will be changing with each new day.

Would it be a viable idea to take the DOBDateTimePicker min date property and increment it somehow each new day, with it preset to 03/05/1990 (18 years back from today).

Or is there a better way to go about this?
 
This date 18 years ago:
VB.NET:
Dim d As Date = Date.Now
d = New Date(d.Year - 18, d.Month, d.Day)
 
Thanks i was able to work that perfectly, I need to show use of an error provider so working that in to an If statement sorted it :)
 
This date 18 years ago:
VB.NET:
Dim d As Date = Date.Now
d = New Date(d.Year - 18, d.Month, d.Day)
VB.NET:
Dim d As Date = Date.Today.AddYears(-18)
 
VB.NET:
Dim d As Date = Date.Today.AddYears(-18)

This is also a slightly safer way than John's way. If you, for example, do
VB.NET:
        Dim d As Date = New Date(2008, 3, 31)
        d = New Date(d.Year, d.Month + 1, d.Day)
it will throw an ArgumentOutOfRangeException as the date is invalid - April does not have 31 days.

Running it as
VB.NET:
        Dim d As Date = New Date(2008, 3, 31)
        d = d.AddMonths(1)
will return 30th April 2008.
 
Yes, I missed the AddYears method because I didn't think it'd span years, TimeSpan doesn't. But John's ways is not to add 1 to a Month or a Day, at least not without validating first. There's no end to years in either direction, except those that also AddYears raises ArgumentOutOfRangeException for.
 
Back
Top