Hello Everyone and thanks for your help in advance. I am working on an application that needs to determine whether a person has passed their 18th birthday. I tried the following code:
However, when I run this code today (2/16/2012), it returns the number 18. Obviously, this person's 18th birthday has not yet passed so this code will not work. I'm not sure what the problem is. Any help would be greatly appreciated.
First off, not sure how you got that code to return 18, it returns 215. Secondly, if you truely want to see if they are over 18 then you should try something like this:
Dim eightTeenYearsAgo As Date = DateTime.Now.AddYears(-18)
If DOB > eightTeenYearsAgo Then
Label1.Text = "under 18"
Else
Label1.Text = "over 18"
End If
The problem before was it was rounding the 17 years 11 months to 18 years because the DateDiff() method returns a Long which is considered an integer.
Remember to mark as answer if this post answered or solved your problem.
Marked as answer by Dino He - MSFT on Feb 23, 2012 05:35 AM
Dim years As Integer = DateTime.Now.Year - Calendar1.SelectedDate.Year
If DateTime.Now.Month < Calendar1.SelectedDate.Month Or (DateTime.Now.Month = Calendar1.SelectedDate.Month And DateTime.Now.Day < Calendar1.SelectedDate.Day) Then
years = years - 1
End If
TextBox1.Text = years
A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
kmcnet
Participant
1090 Points
652 Posts
Calculating Age
Feb 16, 2012 09:28 PM|LINK
Hello Everyone and thanks for your help in advance. I am working on an application that needs to determine whether a person has passed their 18th birthday. I tried the following code:
Dim DOB As Date = "3/25/1994"
Dim TodaysDate As Date = Now().ToShortDateString
YearsOld = DateDiff(DateInterval.Month, DOB, TodaysDate)
However, when I run this code today (2/16/2012), it returns the number 18. Obviously, this person's 18th birthday has not yet passed so this code will not work. I'm not sure what the problem is. Any help would be greatly appreciated.
b471code3
Star
13877 Points
2598 Posts
Re: Calculating Age
Feb 16, 2012 10:47 PM|LINK
First off, not sure how you got that code to return 18, it returns 215. Secondly, if you truely want to see if they are over 18 then you should try something like this:
Dim eightTeenYearsAgo As Date = DateTime.Now.AddYears(-18) If DOB > eightTeenYearsAgo Then Label1.Text = "under 18" Else Label1.Text = "over 18" End IfThe problem before was it was rounding the 17 years 11 months to 18 years because the DateDiff() method returns a Long which is considered an integer.
Ramesh T
Contributor
5079 Points
821 Posts
Re: Calculating Age
Feb 16, 2012 10:49 PM|LINK
Hello Mate,
You could calculate like this
venkatmca008
Participant
1810 Points
341 Posts
Re: Calculating Age
Feb 16, 2012 11:33 PM|LINK
HI TRY THIS....
Dim years As Integer = DateTime.Now.Year - Calendar1.SelectedDate.Year If DateTime.Now.Month < Calendar1.SelectedDate.Month Or (DateTime.Now.Month = Calendar1.SelectedDate.Month And DateTime.Now.Day < Calendar1.SelectedDate.Day) Then years = years - 1 End If TextBox1.Text = yearsMicrosoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
limryan3
Member
2 Points
1 Post
Re: Calculating Age
Mar 10, 2012 09:40 AM|LINK
Thanks for your code snipset. I really helps me alot.
central heating