1, 根据年,月 找出这个月的所有周,和这些周的具体开始日期和结束日期:
/// <summary>
/// old 根据年,月,周,计算具体的某一天
/// </summary>
public void showWeekDate(string DataCondiction)
{
int year = 0;
int month = 0;
if (DataCondiction != null || DataCondiction != "")
{
year = int.Parse(DataCondiction.Split('-')[0]);//获取从前台传过来的年份
month = int.Parse(DataCondiction.Split('-')[1]);//获取从前台传过来的年份
}
List<dynamic> lists = new List<dynamic>();
string str = "";
//当前月第一天
DateTime weekStart = new DateTime(year, month, 1);
//该月的最后一天
DateTime monEnd = weekStart.AddMonths(1).AddDays(-1);
int i = 1;
//当前月第一天是星期几
int dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d"));
//该月第一周结束日期
DateTime weekEnd = dayOfWeek == 0 ? weekStart : weekStart.AddDays(7 - dayOfWeek);
str = "第" + i + "周(" + weekStart.ToString("yyyy-MM-dd") + "/" + weekEnd.ToString("yyyy-MM-dd") + ")";
var dateSpanWeek1 = new { ID = 1, Name = str };
lists.Add(dateSpanWeek1);
//当日期小于或等于该月的最后一天
while (weekEnd.AddDays(1) <= monEnd)
{
i++;
//该周的开始时间
weekStart = weekEnd.AddDays(1);
//该周结束时间
weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7);
str = "第" + i + "周(" + weekStart.ToString("yyyy-MM-dd") + "/" + weekEnd.ToString("yyyy-MM-dd") + ")";
var dateSpanWeek2 = new { ID = i, Name = str };
lists.Add(dateSpanWeek2);
}
string ResJsonStr = "";
if (lists.Count > 0)
{
ResJsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(lists);
}
else
{
ResJsonStr += "[]";
}
this.Response.Write(ResJsonStr.ToString());
Response.End();
}
2,根据 年 来 算出 所有的周 (53 周 )(上个星期日~这个星期六 是一周)
关键点就是根据开始日期计算第一周的时间,比如2013-1-1周二,那么就在周的基础上减去1天,
等于起点就到2012-12-30了,然后在这个基础上+6,完成第一周的计算,剩下的就直接在计算后的日期上相加,
由于个人对算法的知识了解的少,请教同事后得出该算法,感谢我的同事。
List<dynamic> list2 = new List<dynamic>();
DateTime startDate = DateTime.Parse("2014" + "-01-01");
DateTime endDate = DateTime.Parse("2014" + "-12-31");
TimeSpan ts = (TimeSpan)(endDate - startDate);
//总天数
int countday = int.Parse(ts.TotalDays.ToString());
//开始日期星期几
int startweek = Convert.ToInt32(startDate.DayOfWeek.ToString("d"));
//星期日为0改为7
startweek = startweek == 0 ? 7 : startweek;
//把第一周凑满的开始日期
DateTime startDate2 = startDate.AddDays(-startweek + 1);
//再计算总天数
countday += startweek - 1;
double countweek = Math.Ceiling((double)countday / 7);
if (countweek > 0)
{
list2.Add(new { ID = 1, Name = "第1周(" + startDate.ToString("yyyy-MM-dd") + "/" + (startDate2.AddDays(6)).ToString("yyyy-MM-dd") + ")" });
}
for (int w = 2; w <= countweek; w++)
{
if (w != countweek)
{
list2.Add(new { ID = w, Name = "第" + w.ToString() + "周(" + startDate2.AddDays(7 * (w - 1)).ToString("yyyy-MM-dd") + "/" + (startDate2.AddDays((7 * w) - 1).ToString("yyyy-MM-dd")) + ")" });
}
else
{
list2.Add(new { ID = w, Name = "第" + w.ToString() + "周(" + startDate2.AddDays(7 * (w - 1)).ToString("yyyy-MM-dd") + "/" + (endDate.ToString("yyyy-MM-dd")) + ")" });
}
}
ddlHoursdistribution.DataSource = list2;
ddlHoursdistribution.DataBind();
有一表单:
课程 时间
chinese 2009-06-20
physics 2009-06-22
....
math 2009-10-20
chemistry 2009-10-16
....
现在需要按周查询,比如将第20周所有记录读出,如何查询,我用to_char,但ms的sql不支持这个函数!
哪位高手指点一下,谢谢了!!
更多0
where datepart(wk,时间)=20