Results 1 to 7 of 7

Thread: Calculate years & months [Resolved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Resolved Calculate years & months [Resolved]

    I'm using DateDiff in a query to calculate the time between two dates. Right now it's giving me the whole number of years. Is there a way to get the years and months, i.e. 2 yrs 3 months, 2.3, or similar? Thanks.
    Last edited by salvelinus; Mar 2nd, 2005 at 02:53 PM.
    Tengo mas preguntas que contestas

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Calculate years & months

    A Possible solution :
    Code:
    cdbl(format(cdate("01 jan 2005")-cdate("01 jul 2002"),"yyyy.m"))-1900

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Calculate years & months

    Thanks, that's pretty close. Two issues:
    Some results have multiple zeroes after the decimal point, e.g. 2.20000000,
    and the results are in base 10 for the months, whereas there are 12 months in the year. I'm thinking a Mod is needed, but can't figure out where it would go. Here's what I have:
    VB Code:
    1. SELECT tblBMXData.fldDepartment, tblBMXData.fldPhone, tblBMXData.fldLName, _
    2. tblBMXData.fldFName, tblBMXData.fldTitle, tblBMXData.fldHireDate, _
    3. tblBMXData.fldTermDate, cdbl(format(cdate(fldTermDate) - cdate(fldHireDate), 'yyyy.m')) - 1900 AS fldSvcYrs
    4. FROM tblBMXData
    5. ORDER BY fldTermDate;
    Tengo mas preguntas que contestas

  4. #4
    Junior Member
    Join Date
    Mar 2005
    Location
    Kaiserslautern, Germany
    Posts
    25

    Re: Calculate years & months

    Hi...

    here s a snippet how you might calculate the difference as desired

    VB Code:
    1. sub test
    2.   Dim dt1 As Date
    3.   Dim dt2 As Date
    4.   dt1 = #1/1/2005#
    5.   dt2 = #12/1/2004#
    6.   Debug.Print Abs(Fix(DateDiff("m", dt1, dt2) / 12)) & "." & Abs(DateDiff("m", dt1, dt2) Mod 12)
    7. end sub

    and here within your query supposing you are using a query within ms-jet

    VB Code:
    1. SELECT tblBMXData.fldDepartment, tblBMXData.fldPhone, tblBMXData.fldLName, _
    2. tblBMXData.fldFName, tblBMXData.fldTitle, tblBMXData.fldHireDate, _
    3. tblBMXData.fldTermDate,
    4. Abs(Fix(DateDiff("m", fldTermDate, fldHireDate) / 12)) & "." & Abs(DateDiff("m", fldTermDate, fldHireDate) Mod 12) as fldSvcYrs
    5. FROM tblBMXData
    6. ORDER BY fldTermDate;

    Hope this helps

    Regards

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Calculate years & months

    Welcome to the Forums.

    Your test sub will return a value of 0.1 or 1/10 of a year? Not 0.08333 or 1/12 of a year?
    I think its on the right track. Let me see what I can come up with.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Junior Member
    Join Date
    Mar 2005
    Location
    Kaiserslautern, Germany
    Posts
    25

    Re: Calculate years & months

    ok... i forgot to tell that the number before the dot displays the amount of years and the number after the dot the amount of months (As specified by salvenius...)
    Last edited by ivobenkovic; Mar 2nd, 2005 at 02:24 PM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Calculate years & months

    That works great. I'd never heard of the Fix function, and always think of Abs as math stuff. Thanks to all of you.
    Tengo mas preguntas que contestas

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