john true
john true

Reputation: 273

Datetime in For loop

I have a for loop in date time how ever counter doesn't increase. I didn't see the reason. Can you help me on it?

private void ubGO_Click(object sender, EventArgs e)
{
    DateTime startDate = udteMin.DateTime.Date;
    DateTime endDate = udteMax.DateTime.Date;

    for (DateTime counter = startDate; counter <= endDate; counter.AddDays(1))
    {
        MessageBox.Show(counter.Date.ToString() + "            " + counter.AddDays(1).Date.ToString());
    }
}

Upvotes: 2

Views: 612

Answers (1)

Jesse Carter
Jesse Carter

Reputation: 21147

AddDays returns a new DateTime object. It doesn't mutate your existing one. You'll need to reassign counter with the result of AddDays

counter = counter.AddDays(1);

Upvotes: 7

Related Questions