获取一个星期时间段的具体日期

经常要需要得出某一日期是星期几,或者需要在一个星期段中获取数据
下面是实现方法,有3段程序,分别为"数据绑定"过程,"上一周"和"下一周"按钮
说明:当点击上一周,求出Session[StartDate]和Session[DueDate],然后根据这个时间段,来求数据.
//iWeek.ToString()为第几周
//dStartDate.Month.ToString()在一个星期段中开始月份
//dStartDate.Day.ToString()在一个星期段中开始日期
//dDueDate.Month.ToString()在一个星期段中结束月份
//dDueDate.Day.ToString()在一个星期段中结束日期
//Session[StartDate]和Session[DueDate]就是时间段了

1.数据绑定过程
private void InitFormData() 
 
{   
   System.Globalization.Calendar ce 
= new GregorianCalendar();
   
int iWeek = ce.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
   
int iDay = (int) DateTime.Now.DayOfWeek;  
    DateTime dStartDate 
= DateTime.Now.AddDays( -(iDay - 1));  
    DateTime dDueDate 
= DateTime.Now.AddDays( -(iDay - 1+ 6 );
   Session[
"StartDate"= dStartDate.ToShortDateString() + " 0:00:00";
   Session[
"DueDate"= dDueDate.ToShortDateString() + " 23:59:59";
}
2.上一周按钮事件
1private void btnPrevious_Click(object sender, System.EventArgs e)  {
2   System.Globalization.Calendar ce = new GregorianCalendar();  
3 DateTime dStartDate = DateTime.Parse(Session["StartDate"].ToString()).AddDays( - 7 );
4   DateTime dDueDate = DateTime.Parse(Session["DueDate"].ToString()).AddDays( - 7 ); 
5  Session["StartDate"= dStartDate.ToShortDateString() + " 0:00:00";
6   Session["DueDate"= dDueDate.ToShortDateString() + " 23:59:59"
7  int iWeek = ce.GetWeekOfYear(dStartDate, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday); 
8 }
3.下一周按钮事件
1private void btnNext_Click(object sender, System.EventArgs e)  
2{   
3System.Globalization.Calendar ce = new GregorianCalendar();
4   DateTime dStartDate = DateTime.Parse(Session["StartDate"].ToString()).AddDays( 7 );
5   DateTime dDueDate = DateTime.Parse(Session["DueDate"].ToString()).AddDays( 7 );  
6 Session["StartDate"= dStartDate.ToShortDateString() + " 0:00:00";  
      Session[
"DueDate"= dDueDate.ToShortDateString() + " 23:59:59";   
7int iWeek = ce.GetWeekOfYear(dStartDate, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
8  }
原文地址:https://www.cnblogs.com/zjy/p/479562.html