To get the mondays to saturdays in the current month

后端 未结 3 1174
别跟我提以往
别跟我提以往 2021-01-07 06:52

In a month i want to know mondays to sataurdays for the current month eg: in oct month 2011 there are

3-oct-2011 to 8-oct-2011,
10-OCt-11 to 15-Oct-11,          


        
3条回答
  •  情书的邮戳
    2021-01-07 07:04

    Efficient solution;

    var x = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
    int i = 1;
    while (i <= x)
    {
        if (new DateTime(DateTime.Now.Year, DateTime.Now.Month, i).DayOfWeek == DayOfWeek.Saturday)
        {
            Console.WriteLine(new DateTime(DateTime.Now.Year, DateTime.Now.Month, i));
            i += 6;
        }
        i++;
    }
    

提交回复
热议问题