To get the mondays to saturdays in the current month

后端 未结 3 1171
别跟我提以往
别跟我提以往 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条回答
  •  萌比男神i
    2021-01-07 07:22

    most easy:

            int month = DateTime.Now.Month;
            int year = DateTime.Now.Year;
            int days= DateTime.DaysInMonth(year, month);
            int totalSaturdays = 0;
            for(int i=1;i<=days;i++)
            {
                var day = new DateTime(year, month, i);
                if(day.DayOfWeek==DayOfWeek.Saturday)
                {
                    totalSaturdays++;
                }
            }
            Console.WriteLine(("Total Saturdays ="+totalSaturdays.ToString()));
            Console.ReadLine();
    

提交回复
热议问题