BotMaster3000
BotMaster3000

Reputation: 482

DateTime substracting days doesn't work

I am using the following code in order to substract a day of the DateTime until I am getting Monday:

DateTime currentWeek = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);                
while (currentWeek.DayOfWeek.ToString() != "Monday")
{
    currentWeek.AddDays(-1);
    MessageBox.Show(currentWeek.Day.ToString());
    MessageBox.Show(currentWeek.DayOfWeek.ToString());
}

beginDate is in the first run set to the current Date of DateTime.Now.

For me this loops forever, and the day of currentWeek always stays the same (29) even though I am substracting 1 everytime I am looping through.

I am already using another function that takes a DateTime and a bool Parameter, which does pretty much the same and works:

private void ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
    string wochentagName;
    int incrementor;
    if(anfangDerWoche == true)
    {
        wochentagName = "Monday";
        incrementor = -1;
    }
    else
    {
        wochentagName = "Friday";
        incrementor = 1;
    }

    while(date.DayOfWeek.ToString() != wochentagName)
    {                
        date = date.AddDays(incrementor);
    }
}                

Can someone explain to me why the upper code doesn't work whilst the lower one does?

Upvotes: 0

Views: 106

Answers (3)

Igor
Igor

Reputation: 62213

You have to assign the resulting value, DateTime is immutable.

currentWeek = currentWeek.AddDays(-1);

About your 2nd question:

  1. Use the enum for day of the week, do not try to convert a day of the week to a string for a comparison. The type is DayOfWeek.
  2. Again, a DateTime is not mutable so you have to return a DateTime instance as you can't mutate the one that was passed in (without passing it as ref)

Code change

private DateTime ErstenTagDerWocheAuswaehlen(DateTime date, bool anfangDerWoche = true)
{
    System.DayOfWeek wochentagName;
    int incrementor;
    if(anfangDerWoche == true)
    {
        wochentagName = System.DayOfWeek.Monday;
        incrementor = -1;
    }
    else
    {
        wochentagName = System.DayOfWeek.Friday;
        incrementor = 1;
    }

    while(date.DayOfWeek != wochentagName)
    {                
        date = date.AddDays(incrementor);
    }
    return date;
}   

Upvotes: 4

Gilad Green
Gilad Green

Reputation: 37299

As DateTime is immutable, when using the AddDays it returns a new DateTime structure with the new information and does not change the given one. Method documentation states:

Returns a new System.DateTime that adds the specified number of days to the value of this instance.

You must assign it to a variable:

currentWeek = currentWeek.AddDays(-1);

Upvotes: 2

Curt Nichols
Curt Nichols

Reputation: 2767

DateTime is an immutable struct, so you need to store the value returned from AddDays():

var t2 = currentWeek.AddDays(-1);

Then use t2. The call to AddDays() doesn't actually change currentWeek.

Upvotes: 2

Related Questions