循环播放... firstdayofprevmonth-> last

时间:2010-03-05 05:39:11

标签: c# .net

为什么我的作品在这里不正确?

               int dayOfWeekNumber = (int)DateTime.Today.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
               var previosWeekFirstDay = DateTime.Today.AddDays(-7 - dayOfWeekNumber);
               var yearsterday = DateTime.Today.AddDays(-1);
               var previosWeekLastDay = previosWeekFirstDay.AddDays(6);
               var previosMonthFirstDay = DateTime.Today.AddMonths(-1);
               previosMonthFirstDay = previosMonthFirstDay.AddDays(-previosMonthFirstDay.Day + 1);
               var previosMonthLastDay = previosMonthFirstDay.AddDays(DateTime.DaysInMonth(previosMonthFirstDay.Year, previosMonthFirstDay.Month) - 1);

                   for (DateTime x = previosMonthFirstDay; x <= previosMonthLastDay; x.AddDays(1))
                   {
                       //forever looping ...
                   }

1 个答案:

答案 0 :(得分:3)

将for循环更改为

for (DateTime x = previosMonthFirstDay; x <= previosMonthLastDay; x = x.AddDays(1))

你必须记住DateTime.AddDays Method那个

  

此方法不会更改该值   这个DateTime。相反,一个新的   返回DateTime,其值为   这个操作的结果。