ASP.NET做日历
一周一周来算,注,如果本月1号是星期日,则这一月共有六周,即六行,所以干脆每月都输出6周就行了,然后再算第一周里的本月一号是周几,第一周里本月又占了几天。。。呃。。。反正很乱,想了一上午,现在写出来了倒不知道如何解释了。。算了。。直接上代码。。
//日历界面 public IActionResult RiLi() { List<Model.VM_OneDay>[] ss = new List<Model.VM_OneDay>[6]; DateTime nowmonth01 = DateTime.Parse( DateTime.Now.ToString("yyyy-MM-01")); //本月一号 int week01 =(int)nowmonth01.DayOfWeek; //本月一号是周几? int zhanjitian = 7 - week01; //本月在第一周占了几天,如3月1日是周三,则第一周则占了5天,用于算第2,3,4周 for (int i = 0; i < 6; i++) { List<Model.VM_OneDay> aa = new List<Model.VM_OneDay>(); if (i == 0) { //第一周 for (int j = 0; j < 7; j++) { if (j>=(int)week01) { int x = j - week01; DateTime date = nowmonth01.AddDays(x); if (x==0) { aa.Add(new Model.VM_OneDay() {Date=date, DateStr = date.ToString("M月d日") }); //1号 } else { aa.Add(new Model.VM_OneDay() {Date = date, DateStr = date.Day.ToString() }); } } else { DateTime date = nowmonth01.AddDays(-(week01-j)); aa.Add(new Model.VM_OneDay() {Date = date, DateStr = date.Day.ToString() }); } } } else { for (int j = 0; j < 7; j++) { int x = (i - 1) * 7 + zhanjitian + j; DateTime date = nowmonth01.AddDays(x); if (date.Day == 1) { aa.Add(new Model.VM_OneDay() {Date = date, DateStr = date.ToString("M月d日") }); } else { aa.Add(new Model.VM_OneDay() {Date = date, DateStr = date.Day.ToString() }); } } } ss[i] = aa; } ViewBag.data = ss; return View(); }
最后出来的界面:
日历静态页:http://www.niunan.net/rili.html
撸码:复制、粘贴,拿起键盘就是“干”!!!