日期:2014-05-17 浏览次数:20972 次
/// <summary> /// 获取指定月份指定周数的开始日期 /// </summary> /// <param name="year">年份</param> /// <param name="month">月份</param> /// <param name="index">周数</param> /// <returns></returns> private DateTime GetStartDayOfWeeks(int year, int month, int index) { if (year < 1600 || year > 9999) { MessageBox.Show("年份超限"); return DateTime .MinValue ; } if (month < 0 || month > 12) { MessageBox.Show("月份错误"); return DateTime .MinValue ; } if (index < 1) { MessageBox.Show("周数错误"); return DateTime.MinValue; } DateTime startMonth = new DateTime(year, month, 1); //该月第一天 int dayOfWeek = 7; if (Convert.ToInt32(startMonth.DayOfWeek.ToString("d")) > 0) dayOfWeek = Convert.ToInt32(startMonth.DayOfWeek.ToString("d")); //该月第一天为星期几 DateTime startWeek = startMonth.AddDays(1 - dayOfWeek); //该月第一周开始日期 //DateTime startDayOfWeeks = startWeek.AddDays((index - 1) * 7); //index周的起始日期 DateTime startDayOfWeeks = startWeek.AddDays(index * 7); //index周的起始日期 if ((startDayOfWeeks - startMonth.AddMonths(1)).Days > 0) //startDayOfWeeks不在该月范围内 { MessageBox.Show("输入周数大于本月最大周数"); return DateTime.MinValue; } return startDayOfWeeks; }
//当前时间 DateTime dt = DateTime.Now; //本月第一天时间 DateTime dt_First = dt.AddDays(1 - (dt.Day)); //获得某年某月的天数 int year = dt.Date.Year; int month = dt.Date.Month; int dayCount = DateTime.DaysInMonth(year, month); //本月最后一天时间 DateTime dt_Last = dt_First.AddDays(dayCount - 1);