- //计算出当前月的第二周的星期三为几号
- static void Main(string[] args)
- {
- int day = GetDay(DateTime.Now.Month);
- Console.WriteLine("{0}月的第二周的星期三是{1}号", DateTime.Now.Month, day);
- Console.ReadKey();
- }
- /// <summary>
- /// 计算month月份的第二周的星期三为几号(每个星期的第一天为星期天)
- /// </summary>
- /// <param name="month">月份</param>
- /// <returns>几号</returns>
- public static int GetDay(int month)
- {
- //获取当前月份的第一天
- string firstDay = DateTime.Now.Year + "-" + month + "-01";
- DateTime dt = Convert.ToDateTime(firstDay);
- int week;
- //计算当前月份第一天是星期几
- switch (dt.DayOfWeek.ToString())
- {
- case "Monday":
- week = 1;
- break;
- case "Tuesday":
- week = 2;
- break;
- case "Wednesday":
- week = 3;
- break;
- case "Thursday":
- week = 4;
- break;
- case "Friday":
- week = 5;
- break;
- case "Saturday":
- week = 6;
- break;
- default:
- week = 0;
- break;
- }
- //计算这个月的第二周的星期三和第一天间隔几天,例如:是星期四的话就是7-4+4=7天
- int days = 7 - week + 4;//星期三是一个星期的第4天,(7-week)为本月第一周还剩多少天
- //获取第二周的星期三是几号
- int day = dt.AddDays(days - 1).Day;
- return day;
- }
复制代码 |