Function needed for calculating elasped months in VBScript

In order to track employee Paid Time Off (PTO) I need an accurate way in vbscript to calculate the number of months elasped between Date1 and Date2.  vbscript's DateDiff() is inaccurate and yields rounding errors.  For example 6/15/2003 -> 6/1/2004 should result in "11", not 12, 6/15/2003 -> 9/1/2003 should give 2 (not 3) and 6/15/2004 -> 6/14/2004 should yield 12.  Has anyone written an accurate function to do this or must I revert to a more powerful language that provides true date manipulations?  I can't be the first person to need this, but I see nothing in the archives.  Thanks in advance.
bmoore7624Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

archrajanCommented:
Dim dtNow, dtThen
Dim Elapsed
    dtNow = "12:30:00"
    dtThen = "09:00:00"
    Elapsed = DateDiff("n", dtThen, dtNow) / 60
    MsgBox Elapsed
0
SMartinHamburgCommented:
May be this helps:
http://www.bitbucketheaven.com/code/age.shtml
From the comments somebody obviously hit the sam problem as you but with years.

Regards
0
Ashley BryantSenior Software EngineerCommented:
OK...try this out...

date1 = "6/15/2003"
date2 = "6/1/2004"

intdaydiff = DateDiff("d",date1,date2)
varmonths = Split(CStr(intdaydiff / 30),".")

Response.Write varmonths(0)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

bmoore7624Author Commented:
This last one (by AshleyBryant) is close to what I'm using now:
    CInt( (DateDiff("s",StartDate,EndDate)/ ( 60 * 60 * 24 * 30) ))
and yeilds similar results (and problems).  Both assume uniform 30 day months, which not accurate.  I need to know that if Date1=2/15/2003 and Date2=3/15/2003 that's a calendar month, even though it is only 28 days.  Likewise 3/5 - 4/4 shouldn't be a calendar month, even though it is 30 days.  That's how payroll works.  I guess I'm looking for calendar months.
0
amit_gCommented:
Try this function ...

function GetDateDiffInMonths(Date1, Date2)
    Dim Months, Date3

    Months = DateDiff("m", Date1, Date2)
    Date3 = DateAdd("m", Months, Date1)
    Date3 = DateAdd("d", -1, Date3)

    If (DateDiff("d", Date2, Date3) > 0) Then
        Months = Months - 1
    End If

    GetDateDiffInMonths = Months
end function

Date1 = "6/15/2003"
Date2 = "6/1/2004"

Date1 = "6/15/2003"
Date2 = "9/1/2003"

Date1 = "6/15/2003"
Date2 = "6/14/2004"

Date1 = "2/15/2003"
Date2 = "3/15/2003"

Months = GetDateDiffInMonths(Date1, Date2)

Call MsgBox(Months)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ashley BryantSenior Software EngineerCommented:
Function CalcPTO(StartDate,FinishDate)
      BaseMonth = DateDiff("m",StartDate,FinishDate)
      If Day(StartDate) > Day(FinishDate) then BaseMonth = BaseMonth - 1
      CalcPTO = BaseMonth
End Function

Date1 = "2/15/2003"
Date2 = "3/15/2003"

ActualPTO = CalcPTO(Date1,Date2)

Response.Write ActualPTO

The only scenario that you posted that this does not account for is getting 12 months between 6/15/2003 and 6/14/2004.  That example you gave didn't seem to fit in with the 3/5 - 4/4 not turning out to be a calendar month.  So, either there was a typo or there are certain exceptions to the rules we've been given so far.
0
bmoore7624Author Commented:
Thanks very much amit_g & AshleyBryant.  Both solutions are elegant and work all the way.  I accepted amit_g's answer since it came in first.  Much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.