How to get the difference of the month from the date specified.
Example:

date1.value="5/1/2010"
date2.vale ="5/31/2011"

it should give a result of 12.

thank you for helping.!

Dani AI

Generated

was right to point people at DateDiff and got it working with the "m" interval. A couple of clarifications that often help others hitting the same snag:

DateDiff counts month boundaries, not "complete months" — so short spans that cross a month boundary can return 1 even if less than a full month has elapsed. The "Object required" error usually means the identifier you tried to use with .Value was not a control/object in scope (or you passed a string instead of a Date). Make sure you pass actual Date values (CDate or DateValue if starting from text) or the control property that contains a DateTime value.

If the goal is "whole months elapsed" (i.e., complete month periods), the simple approach is to compute year/month difference and then adjust by day-of-month, with a special case for end-of-month. The function below implements that logic and handles swapped dates:

Public Function WholeMonthsBetween(ByVal startDate As Date, ByVal endDate As Date) As Long
    If startDate > endDate Then
        WholeMonthsBetween = -WholeMonthsBetween(endDate, startDate)
        Exit Function
    End If

    Dim months As Long
    months = (Year(endDate) - Year(startDate)) * 12 + (Month(endDate) - Month(startDate))

    ' if end day is earlier than start day, final month is not complete
    If Day(endDate) < Day(startDate) Then
        months = months - 1
    ElseIf Day(startDate) = Day(DateSerial(Year(startDate), Month(startDate) + 1, 0)) _
           And Day(endDate) = Day(DateSerial(Year(endDate), Month(endDate) + 1, 0)) Then
        ' both on month-end: keep months as-is
    End If

    WholeMonthsBetween = months
End Function

Tips: strip time-of-day with DateValue when you only care about dates, use DateTimePicker.Value or CDate(TextBox.Text) depending on control, and remember DateDiff/date arithmetic returns a signed result depending on argument order.

Recommended Answers

All 4 Replies

Dim resultdate As Long

        resultdate = DateDiff(DateInterval.Year, date2, date1)

Try this...

Sorry u need months right? try this

resultdate = DateDiff(DateInterval.Month, date1, date2)

Sorry u need months right? try this

resultdate = DateDiff(DateInterval.Month, date1, date2)

Thanks. But i got these error!

"Object required" even if the name of the date control is correct..

msgbox datediff(dateinterval.month,dt1.value,dt2.value)

Thanks. But i got these error!

"Object required" even if the name of the date control is correct..

msgbox datediff(dateinterval.month,dt1.value,dt2.value)

I got it.. These works!

dim getmonthdiff as long

getmonthdiff=datediff("m",dt1,dt2)

msgbox getmonthdiff

thx..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.