/// <summary> /// 根据时段改变时间段 /// </summary> /// <param name="timeIntervalValue">时段值</param> /// <param name="dtpStartTime">起始时间控件</param> /// <param name="dtpEndTime">结束时间控件</param> protected void ChangeTimeRangeByTimeInterval(string timeIntervalValue, DateTimePicker dtpStartTime, DateTimePicker dtpEndTime) { DateTime dateTimeTotay = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); DateTime tempDate; switch (timeIntervalValue) { case "0": // 今天 dtpStartTime.Value = dateTimeTotay; dtpEndTime.Value = dateTimeTotay.AddDays(1).AddMilliseconds(-1.0); break; case "1": // 昨天 tempDate = dateTimeTotay.AddDays(-1); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddDays(1).AddMilliseconds(-1.0); break; case "2": // 前天 tempDate = dateTimeTotay.AddDays(-2); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddDays(1).AddMilliseconds(-1.0); break; case "3": // 本周 int temp = (int)dateTimeTotay.DayOfWeek - 1; int days = temp == -1 ? 6 : temp; dtpStartTime.Value = dateTimeTotay.AddDays(-days); dtpEndTime.Value = dateTimeTotay.AddDays(6 - days).AddDays(1).AddMilliseconds(-1.0); break; case "4": // 上周 temp = (int)dateTimeTotay.DayOfWeek - 1; days = temp == -1 ? 6 : temp; dtpStartTime.Value = dateTimeTotay.AddDays(-days - 7); dtpEndTime.Value = dateTimeTotay.AddDays(-1 - days).AddDays(1).AddMilliseconds(-1.0); break; case "5": // 本月 tempDate = Convert.ToDateTime(dateTimeTotay.ToString("yyyy-MM-01 00:00:00")); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddMonths(1).AddDays(-1).AddDays(1).AddMilliseconds(-1.0); break; case "6": // 上月 tempDate = Convert.ToDateTime(dateTimeTotay.AddMonths(-1).ToString("yyyy-MM-01 00:00:00")); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddMonths(1).AddDays(-1).AddDays(1).AddMilliseconds(-1.0); break; case "7": // 今年 tempDate = Convert.ToDateTime(dateTimeTotay.ToString("yyyy-01-01 00:00:00")); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddYears(1).AddDays(-1).AddDays(1).AddMilliseconds(-1.0); break; case "8": // 去年 tempDate = Convert.ToDateTime(dateTimeTotay.AddYears(-1).ToString("yyyy-01-01 00:00:00")); dtpStartTime.Value = tempDate; dtpEndTime.Value = tempDate.AddYears(1).AddDays(-1).AddDays(1).AddMilliseconds(-1.0); break; case "9": // 近七天 tempDate = dateTimeTotay.AddDays(-6); dtpStartTime.Value = tempDate; dtpEndTime.Value = dateTimeTotay.AddDays(1).AddMilliseconds(-1.0); break; case "10": // 近30天 tempDate = dateTimeTotay.AddDays(-29); dtpStartTime.Value = tempDate; dtpEndTime.Value = dateTimeTotay.AddDays(1).AddMilliseconds(-1.0); break; case "11": //全部时段 dtpStartTime.Value = Convert.ToDateTime("1971-01-01 00:00:00"); dtpEndTime.Value = dateTimeTotay.AddDays(1).AddMilliseconds(-1.0); break; } }