1.课程表框架的构建:
2.项目输入框架的构建:
3.通过算法的构建判断当前是开学第几周
1)代码:
public static int Excute(DateTime dTime) { try { //确定此时间在一年中的位置 var dayOfYear = dTime.DayOfYear; //本学期第一天 var tempDate = new DateTime(dTime.Year, 1, 1); //确定第一天 var tempDayOfWeek = (int)tempDate.DayOfWeek; tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek; //确定星期几 var index = (int)dTime.DayOfWeek; index = index == 0 ? 7 : index; //当前周的范围 DateTime retStartDay = dTime.AddDays(-(index - 1)); DateTime retEndDay = dTime.AddDays(7 - index); //确定当前是第几周 var weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7); if (retStartDay.Year < retEndDay.Year) { weekIndex = 1; } return weekIndex; } catch (Exception ex) { throw new Exception(ex.Message); } } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { dateTimePicker2.Visible = false; button2.Visible = false; label3.Visible = false; DateTime dt = DateTime.Parse(dateTimePicker2.Text); //获取当前时间 DateTime date = DateTime.Parse(dateTimePicker1.Text); string hours = date.Hour.ToString(); string minute = date.Minute.ToString(); string second = date.Second.ToString(); int zhoushu = Excute(date); int zhoushu1 = Excute(dt); MessageBox.Show("本周是开学后第" + Convert.ToString(zhoushu - zhoushu1+1)+"周"); label2.Visible = true; label2.Text = "第" + Convert.ToString(zhoushu - zhoushu1 + 1) + "周"; }
2)运行结果截图: