注意,是该月中的周次,一周最多有6周。
<span style="background-color: rgb(102, 51, 255);">//获取周开始日期和结束日期(周日-周六) public string GetWeekDate(int yearly, int monthly, string weekly) { string dayStr=""; try { DateTime day1 = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-01");//该月第一天日期 int weekOfDay1 = (Int32)day1.DayOfWeek;//该月第一天是周几 int dayCount = DateTime.DaysInMonth(yearly, monthly);//该月天数 int endDay = 0;//结束日 DateTime dateEnd = DateTime.Now;//周结束日期,初始化为当前时间 //7*周次 - 第一周中上月天数 > 本月天数 if (7 * Int32.Parse(weekly) - weekOfDay1 > dayCount) { endDay = 7 * Int32.Parse(weekly) - weekOfDay1 - dayCount; DateTime dateLast = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-" + dayCount.ToString());//该月最后一天日期 dateEnd = dateLast.AddDays(endDay); } //7*周次 - 第一周中上月天数 > 本月天数 if (7 * Int32.Parse(weekly) - weekOfDay1 <= dayCount) { endDay = 7 * Int32.Parse(weekly) - weekOfDay1; dateEnd = DateTime.Parse(yearly.ToString() + "-" + monthly.ToString() + "-" + endDay.ToString()); } DateTime dateStart = dateEnd.AddDays(-6); //周开始日期 dayStr = dateStart.ToString("yyyy-MM-dd") +" ~ " + dateEnd.ToString("yyyy-MM-dd"); } catch (Exception) { throw; } return dayStr; }</span>
调用:var resultWeekDay = GetWeekDate(2015, 4, 5);//4月第5周
结果:2015-04-26 ~ 2015-05-02
时间: 2024-10-03 02:10:09