我试图获得每个月星期六的天数。例如,这个月(八月)应该返回:4, 11, 18, 28int year = 2018;int month = DateTime.Now.Month;DateTime myDT = new DateTime(year, month, 1, new GregorianCalendar());Calendar myCal = CultureInfo.InvariantCulture.Calendar;while (myDT.Month == month){ var day = myCal.GetDayOfWeek(myDT); if (day == DayOfWeek.Saturday || day == DayOfWeek.Friday) Debug.WriteLine(myCal.GetDayOfMonth(myDT)); myDT.AddDays(1);}单击执行代码的按钮后,我的 UI 将被冻结,不会返回任何错误。这让我相信它出于某种原因仍在循环中。
2 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
尝试这个:
const int year = 2018;
int month = DateTime.Now.Month;
Calendar myCal = CultureInfo.InvariantCulture.Calendar;
DayOfWeek[] accepted = new [] { DayOfWeek.Saturday, DayOfWeek.Friday };
IEnumerable<DateTime> dateTimes = Enumerable.Range(1, DateTime.DaysInMonth(year, month))
.Select(day => new DateTime(year, month, day))
.Where(d => accepted.Contains(myCal.GetDayOfWeek(d)));
添加回答
举报
0/150
提交
取消