I am facing a problem, logic written in my program is below
while (lastDate.Month < DateTime.Today.Month - 1)//
{
lastDate= lastDate.AddMonths(1);
list.Add(lastDate);
}
This code is failing when lastDate month is Dec and i am executing this code in Jan or Feb of new year because 12 would never be greater then 1 0r 2.
I need to write a logic where my loop could traverse through Nov, Dec, Jan , Feb and so on.
I have written below code which is working however i am not getting clue to exit, loop should exit when difference between lastDate and todays date is 2 months.
if (lastDate.Month > DateTime.Today.Month && lastDate.Year < DateTime.Today.Year)
{
while (lastDate.Year <= DateTime.Today.Year)
{
lastDate= lastDate.AddMonths(1);
list.Add(lastDate);
}
}
Please help me in this