Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate months difference between two dates 1

Status
Not open for further replies.

swetham

Programmer
May 5, 2010
257
0
0
DE
How to calculate the months difference between two dates. For example,

if from_date='2011-07-06', to_date='2012-01-01", months should come as 5 and days=days between (2011-12-06 and 2012-01-01)

if from_date='2011-07-06", to_date='2012-01-06", months should come as 6.

Can anyone please help me out form this issue?
 

Here's a little something I whipped up using the DateAdd and DateDiff functions:

Code:
        Dim d1 As Date
        Dim d2 As Date
        Dim d3 As Date

        d1 = #7/6/2011#
        d2 = #1/1/2012#

        Dim i As Integer = 0

        Do
            i += 1
            d3 = DateAdd(DateInterval.Month, i, d1)
            If d3 > d2 Then Exit Do
        Loop

        i -= 1 'decrement by 1 to get number of months without exceeding d2

        MsgBox(i)

        d3 = DateAdd(DateInterval.Month, i, d1)

        MsgBox(d3)

        MsgBox(DateDiff(DateInterval.Day, d3, d2))


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you so much it is working fine now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top