Results 1 to 5 of 5

Thread: Attention anyone who can help with the datediff function

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Medford Oregon
    Posts
    2

    Exclamation 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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Something like this:

    VB Code:
    1. Dim Date1 As Date = Date.Parse("11/22/1974")
    2.         Dim Date2 As Date = Today
    3.         Dim age As Long = DateDiff(DateInterval.Day, Date1, Date2)
    4.         MessageBox.Show(age \ 365)

    Helps?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Or rather, use this from www.freevbcode.com:

    VB Code:
    1. Public Function GetAge(ByVal Birthdate As System.DateTime, _
    2.     Optional ByVal AsOf As System.DateTime = #1/1/1700#) _
    3.     As String
    4.  
    5.         'Don't set second parameter if you want Age as of today
    6.  
    7.         'Demo 1: get age of person born 2/11/1954
    8.         'Dim objDate As New System.DateTime(1954, 2, 11)
    9.         'Debug.WriteLine(GetAge(objDate))
    10.  
    11.         'Demo 1: get same person's age 10 years from now
    12.         'Dim objDate As New System.DateTime(1954, 2, 11)
    13.         'Dim objdate2 As System.DateTime
    14.         'objdate2 = Now.AddYears(10)
    15.         'Debug.WriteLine(GetAge(objDate, objdate2))
    16.  
    17.         Dim iMonths As Integer
    18.         Dim iYears As Integer
    19.         Dim dYears As Decimal
    20.         Dim lDayOfBirth As Long
    21.         Dim lAsOf As Long
    22.         Dim iBirthMonth As Integer
    23.         Dim iAsOFMonth As Integer
    24.  
    25.         If AsOf = "#1/1/1700#" Then
    26.             AsOf = DateTime.Now
    27.         End If
    28.         lDayOfBirth = DatePart(DateInterval.Day, Birthdate)
    29.         lAsOf = DatePart(DateInterval.Day, AsOf)
    30.  
    31.         iBirthMonth = DatePart(DateInterval.Month, Birthdate)
    32.         iAsOFMonth = DatePart(DateInterval.Month, AsOf)
    33.  
    34.         iMonths = DateDiff(DateInterval.Month, Birthdate, AsOf)
    35.  
    36.         dYears = iMonths / 12
    37.  
    38.         iYears = Math.Floor(dYears)
    39.  
    40.         If iBirthMonth = iAsOFMonth Then
    41.             If lAsOf < lDayOfBirth Then
    42.                 iYears = iYears - 1
    43.             End If
    44.         End If
    45.  
    46.         Return iYears
    47.     End Function

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    Medford Oregon
    Posts
    2

    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

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Place the function in your code's form, and modify your existing code like so:

    VB Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width