猿问

获取一个月的星期六

我试图获得每个月星期六的天数。


例如,这个月(八月)应该返回:4, 11, 18, 28


int 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 将被冻结,不会返回任何错误。这让我相信它出于某种原因仍在循环中。


繁星coding
浏览 160回答 2
2回答

芜湖不芜

你没有分配它,所以它可能处于一个永恒的循环中:取而代之的是:myDT.AddDays(1);做这个:myDT = myDT.AddDays(1);

婷婷同学_

尝试这个:&nbsp; &nbsp; const int year = 2018;&nbsp; &nbsp; int month = DateTime.Now.Month;&nbsp; &nbsp; Calendar myCal = CultureInfo.InvariantCulture.Calendar;&nbsp; &nbsp; DayOfWeek[] accepted = new [] { DayOfWeek.Saturday, DayOfWeek.Friday };&nbsp; &nbsp; IEnumerable<DateTime> dateTimes = Enumerable.Range(1, DateTime.DaysInMonth(year, month))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .Select(day => new DateTime(year, month, day))&nbsp; &nbsp; &nbsp; &nbsp; .Where(d => accepted.Contains(myCal.GetDayOfWeek(d)));
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答