DateAdd question


CG Rosén

Good day Group,

Have some problem to understand the DateAdd function.
Seems that if the start date is a month with a last date of 28 and 30
the DateAdd function does not add a month in the way that the following
month will be shown with its last date. It works if the start date is a
that with a last date of 31.
The problem can be checked by following code.
Is this the way it is supposed to be or am I doing this the wrong way?

str = "2006-02-28"

For i = 1 To 5

d = DateAdd("m", i, str)

MsgBox d

Next i


CG Rosen




Adding a 1 to the number of the month of the date is a reasonable
interpretation, but obviously not what you want.

This represents a way to get the last day. You would have to add your own
logic to determine which approach to use when.

Sub AA()
Dim dStr As Date
Dim d As Date, d1 As Date
dStr = DateSerial(2006, 2, 28)

For i = 1 To 5

d = DateAdd("m", i, dStr)
d1 = DateSerial(Year(dStr), Month(dStr) + i + 1, 0)

MsgBox d & vbNewLine & d1

Next i

End Sub

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question