Results 1 to 4 of 4

Thread: Calculating Months

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129

    Calculating Months

    Hi All,

    I need to be able to calculate the age of a person in both years and months. The years I have sorted but the code for working out the number of months does not. It appears to work for the first 5 months but after that it goes weird.

    VB Code:
    1. 'get the age in months
    2.             Dim date1 As Date
    3.             Dim date2 As Date
    4.             Dim mnths As Integer
    5.             Dim yrs As Integer
    6.             Dim mnthcalc As Integer
    7.             'set date to today
    8.             date1 = Now
    9.             date2 = dtDOB.Value
    10.  
    11.             mnths = DateDiff(DateInterval.Month, date2, date1)
    12.             yrs = DateDiff(DateInterval.Year, date2, date1)
    13.  
    14.             If yrs = 0 Then
    15.                 txtMonths.Text = mnths
    16.             Else
    17.                 'mnthcalc = yrs * 12
    18.                 'txtMonths.Text = (mnths - mnthcalc)
    19.                 mnthcalc = mnths / 12
    20.                 txtMonths.Text = mnthcalc
    21.             End If

    any help appreciated.


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calculating Months

    I'd suggest that you use the DateTime and TimeSpan structures to do this calculation rather than using the old DateDiff function. Haven't time to implement right now but I'll be back within a few hours.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129

    Re: Calculating Months

    Cheers,
    I haven't used those before, an example would be greatly appreciated.
    Cheers

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calculating Months

    Actually, you lie. You have used the DateTime structure before because the in-built Date data type is actually implemented using the DateTime structure. Every Date object is a DateTime structure. We'll forget the TimeSpan for now as it's not needed for this example. Assuming that the person's date of birth is stored in a Date variable called dtDOB, you would get their current age in whole months like this:
    VB Code:
    1. Dim ageInMonths As Integer = (Date.Now.Year - dtDOB.Year) * 12 + (Date.Now.Month - dtDOB.Month)
    2.  
    3.         If Date.Now.Day < dtDOB.Day Then
    4.             'The current month is yet to be completed.
    5.             ageInMonths -= 1
    6.         End If

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