|
-
Nov 22nd, 2004, 01:49 AM
#1
Thread Starter
New Member
Attention anyone who can help with the datediff function
Hi everyone I am hoping somebody can help me...
I have a project due in my VB.net class and I am not seeing what I need in the index files or anywhere else for that matter
What I need specifically is to calculate someones age in years by their exact birthdate
But here is the kicker...When I use this
btnCalc_click....etc...
Privatesub
Dim Date1 As Date = CDate(txtBirthDate.Text)
Dim Date2 As Date = Today
Dim age As Long = DateDiff(DateInterval.Year, Date1, Date2)
lblage.Text = CStr(age)
end sub
it returns the number in years but does not take in to consideration that if the month has not occured yet in our present year then the person is infact not the age that is being displayed in lblage.text
It only counts the years and ignores the days and months
if I change ...(DateInterval.year....) to ...(DateInterval.day...)then it returns a count of the days from the txtbirthdate.text
So basicaly when I enter a date of 12/22/1974
the program returns 30 years when (if your reading this prior to 12/22/2004) It shoud return 29.
This cannot be alowed to continue or there will be chaos throughout the land please help.
I know this is probably a stupid question to the experienced programmers here, but I am hopfull that one of you could take five minutes and shoot me some code that would work
thanks in advance
Warpig
-
Nov 22nd, 2004, 02:03 AM
#2
Something like this:
VB Code:
Dim Date1 As Date = Date.Parse("11/22/1974")
Dim Date2 As Date = Today
Dim age As Long = DateDiff(DateInterval.Day, Date1, Date2)
MessageBox.Show(age \ 365)
Helps?
-
Nov 22nd, 2004, 02:19 AM
#3
Or rather, use this from www.freevbcode.com:
VB Code:
Public Function GetAge(ByVal Birthdate As System.DateTime, _
Optional ByVal AsOf As System.DateTime = #1/1/1700#) _
As String
'Don't set second parameter if you want Age as of today
'Demo 1: get age of person born 2/11/1954
'Dim objDate As New System.DateTime(1954, 2, 11)
'Debug.WriteLine(GetAge(objDate))
'Demo 1: get same person's age 10 years from now
'Dim objDate As New System.DateTime(1954, 2, 11)
'Dim objdate2 As System.DateTime
'objdate2 = Now.AddYears(10)
'Debug.WriteLine(GetAge(objDate, objdate2))
Dim iMonths As Integer
Dim iYears As Integer
Dim dYears As Decimal
Dim lDayOfBirth As Long
Dim lAsOf As Long
Dim iBirthMonth As Integer
Dim iAsOFMonth As Integer
If AsOf = "#1/1/1700#" Then
AsOf = DateTime.Now
End If
lDayOfBirth = DatePart(DateInterval.Day, Birthdate)
lAsOf = DatePart(DateInterval.Day, AsOf)
iBirthMonth = DatePart(DateInterval.Month, Birthdate)
iAsOFMonth = DatePart(DateInterval.Month, AsOf)
iMonths = DateDiff(DateInterval.Month, Birthdate, AsOf)
dYears = iMonths / 12
iYears = Math.Floor(dYears)
If iBirthMonth = iAsOFMonth Then
If lAsOf < lDayOfBirth Then
iYears = iYears - 1
End If
End If
Return iYears
End Function
-
Nov 22nd, 2004, 03:25 AM
#4
Thread Starter
New Member
thx still need some help though
the first post doesn't work becaus its just using the integer division operator and so its rounding the numbers accordingly and spitting out a closer number but it can still get the wrong one(believe me when I say was very hopefull that it would be that easy)
However I looked at the second one and I am not sure how to implement it.
My level of programming is so far fairly limited to the btn calc button. So I put the whole thing into the btncalc section of my code and I am getting some large decimal returns.
I'm not quite seeing how to make it work
sorry to be a bother but still in need of some guidance.
thx in advance
Warpig
-
Nov 22nd, 2004, 03:40 AM
#5
Place the function in your code's form, and modify your existing code like so:
VB Code:
lblAge.Text = GetAge(Date.Parse(txtBirthDate.Text))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|