Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DateTime evaluation problem

So I'm trying to figure out what I'm doing wrong with this logic. It seems straightforward and my breakpoints indicate that the evaulation in the 'if' statement is resolving as True, but sum.ppStart et al aren't getting 14 days added to them.

It's probably something simple, but any help would be appreciated.

//Determine the start/end days of each week of the pay period and retrieve a list of those entries

DateTime[] weeks = timeTools.calcPP(0);
DateTime today = DateTime.Now.Date;

if (today > weeks[3])
{
  weeks[0].AddDays(14);
  weeks[3].AddDays(14);
  weeks[4].AddDays(14);
}

sum.ppStart = weeks[0];
sum.ppEnd = weeks[3];
sum.payDate = weeks[4];
like image 366
octopusbones Avatar asked Jul 10 '26 09:07

octopusbones


2 Answers

AddDays returns a new instance of DateTime, the existing value is not changed, it is an immutable structure. When using the function, capture the result

DateTime myDate = ...
myDate = myDate.AddDays(14);
like image 101
Anthony Pegram Avatar answered Jul 14 '26 18:07

Anthony Pegram


That is because you're not using the result of the AddDays method. The signature is

 public DateTime AddDays(double days) 

or so (see link). You need to do this:

weeks[0] = weeks[0].AddDays(14);
like image 22
Tejs Avatar answered Jul 14 '26 17:07

Tejs