E R
E R

Reputation: 490

how to loop over dates step one month with vb.net

how to use for next with dates in VB.Net

I need to say something like this

For dt As Date = (someDate) to Today().Date step 1 Month

' do something

next

Upvotes: 1

Views: 1372

Answers (1)

nelek
nelek

Reputation: 4312

Try bellow code (using Do While...Loop) :

Label1.Text = ""
Dim somedate As Date = CDate("2017-1-16") 'this is Your somedate
Do While somedate <= Today.Date
    'do something, in this case show date in label
    Label1.Text += String.Format("{0:yyyy-MM-dd}", somedate) + vbCrLf
    'after do something increase somedate for one month
    somedate = somedate.AddMonths(1)
Loop

It's important You first do something and then increase Your somedate, otherwise, loop will pass Today.Date and will "jump" to next month after Today.Date.

Upvotes: 2

Related Questions