Datetime in a For loop

I have a for loop on a date that never counter

increases. I didn't see the reason. Can you help me?

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());
    }
}

      

+3


source to share


1 answer


AddDays

returns a new DateTime object. It does not mutate your existing one. You will need to remap the counter using AddDays



counter = counter.AddDays(1);

      

+6


source







All Articles