Subtract 1 month from current month

The following code does not work. It should "do something" but it does not. I'm trying to subtract 1 month from current month and compare it to a string variable pulled from database. The string variable strCCMonth is '8', so it should do something since the current month is '9'.

Thank you.
Dim monthadd As Date = DateAdd("m", -1, Date.Now)
        If strCCMonth = monthadd.ToString() Then
           Do Something here
        End If

Open in new window

LVL 1
mlg101Asked:
Who is Participating?
 
ZhaolaiCommented:
Try this:

        Dim monthadd As Date = DateAdd("m", -1, Date.Now)
        If strCCMonth = monthadd.Month.ToString() Then
            'Do Something here
        End If

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
try Dim SubMonth As Integer = Today.Month - 1
0
 
EYoungCommented:
Put a break on the "Do Something here" line and when the program stops, put the cursor over each of the two values, i.e. strCCMonth and monthadd.ToString().  That will show you the value in each and the field sizes, blank spaces, etc.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZhaolaiCommented:
A simpler way:

        If strCCMonth = Now.AddMonths(-1).ToString() Then
            'Do Something here
        End If

Open in new window

0
 
käµfm³d 👽Commented:
A bit simpler, I think:
Dim monthadd As Date = Now.AddMonths(-1)

If Convert.ToInt32(strCCMonth) = monthadd.Month Then
    'Do Something
End If

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
....
Dim SubMonth As Integer = Today.Month - 1
If strCCMonth = SubMonth.ToString() Then
            'Do Something here
 End If

Open in new window

0
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.

All Courses

From novice to tech pro — start learning today.