最近一周的日期选择设置

 private void radioGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (radioGroup.SelectedIndex)
            {
                //当天
                case 0:
                    {
                        this.dateStart.DateTime = DateTime.Today;
                        this.dateEnd.DateTime = DateTime.Today;
                        break;
                    }
                //本周
                case 2:
                    {
                        int weeknow = Convert.ToInt32(System.DateTime.Now.DayOfWeek);
                        //星期日 获取weeknow为0 
                        weeknow = weeknow == 0 ? 7 : weeknow;
                        int daydiff = (-1) * weeknow + 1;
                        int dayadd = 7 - weeknow;
                        //本周第一天 
                        this.dateStart.DateTime = DateTime.Now.AddDays(daydiff);
                        //本周最后一天 
                        this.dateEnd.DateTime = DateTime.Now.AddDays(dayadd);
                        break;
                    }
                //上周
                case 4:
                    {
                        DateTime dt = DateTime.Now;  //当前时间
                        int dayOfWeek = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
                        DateTime weekStart = dt.AddDays(1 - ((dayOfWeek == 0) ? 7 : dayOfWeek));   //本周周一
                        DateTime weekEnd = weekStart.AddDays(6);  //本周周日
                        this.dateStart.DateTime = weekStart.AddDays(-7);  //上周周一
                        this.dateEnd.DateTime = weekEnd.AddDays(-7);  //上周周日
                        break;
                    }
                //本月
                case 1:
                    {
                        this.dateStart.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                        this.dateEnd.DateTime = this.dateStart.DateTime.AddMonths(1).AddDays(-1);
                        break;
                    }
                //上月
                case 3:
                    {
                        DateTime lastMonth = DateTime.Now.AddMonths(-1);
                        this.dateStart.DateTime = lastMonth.AddDays(1 - lastMonth.Day);
                        this.dateEnd.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
                        break;
                    }
                //本年
                case 5:
                    {
                        this.dateStart.DateTime = new DateTime(DateTime.Now.Year, 1, 1);
                        this.dateEnd.DateTime = new DateTime(DateTime.Now.Year, 12, 31);
                        break;
                    }
            }
            //this.GetQuery();
        }

 

效果 图

posted @ 2017-01-11 18:50  龙骑科技  阅读(419)  评论(0编辑  收藏  举报