|
-
Mar 2nd, 2005, 08:54 AM
#1
Thread Starter
Frenzied Member
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
-
Mar 2nd, 2005, 10:12 AM
#2
Re: Calculate years & months
A Possible solution :
Code:
cdbl(format(cdate("01 jan 2005")-cdate("01 jul 2002"),"yyyy.m"))-1900
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...
-
Mar 2nd, 2005, 10:36 AM
#3
Thread Starter
Frenzied Member
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:
SELECT tblBMXData.fldDepartment, tblBMXData.fldPhone, tblBMXData.fldLName, _
tblBMXData.fldFName, tblBMXData.fldTitle, tblBMXData.fldHireDate, _
tblBMXData.fldTermDate, cdbl(format(cdate(fldTermDate) - cdate(fldHireDate), 'yyyy.m')) - 1900 AS fldSvcYrs
FROM tblBMXData
ORDER BY fldTermDate;
Tengo mas preguntas que contestas
-
Mar 2nd, 2005, 02:02 PM
#4
Junior Member
Re: Calculate years & months
Hi...
here s a snippet how you might calculate the difference as desired
VB Code:
sub test
Dim dt1 As Date
Dim dt2 As Date
dt1 = #1/1/2005#
dt2 = #12/1/2004#
Debug.Print Abs(Fix(DateDiff("m", dt1, dt2) / 12)) & "." & Abs(DateDiff("m", dt1, dt2) Mod 12)
end sub
and here within your query supposing you are using a query within ms-jet
VB Code:
SELECT tblBMXData.fldDepartment, tblBMXData.fldPhone, tblBMXData.fldLName, _
tblBMXData.fldFName, tblBMXData.fldTitle, tblBMXData.fldHireDate, _
tblBMXData.fldTermDate,
Abs(Fix(DateDiff("m", fldTermDate, fldHireDate) / 12)) & "." & Abs(DateDiff("m", fldTermDate, fldHireDate) Mod 12) as fldSvcYrs
FROM tblBMXData
ORDER BY fldTermDate;
Hope this helps
Regards
-
Mar 2nd, 2005, 02:16 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 2nd, 2005, 02:20 PM
#6
Junior Member
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.
-
Mar 2nd, 2005, 02:53 PM
#7
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|