要求大概就是,选定一段时间周期,勾选周日至周六,把时间周期符合要求的日期返回,例如:勾选了周日,就把时间周期内所有是周日的日期返回,勾选了周日和周五,就把时间周期内所有周五和周日的日期返回。重复间隔是以时间周期内的星期为单位进行跳选,间隔为0时,每个星期都遍历,间隔为1时,遍历完第一周,跳到第三周再遍历,间隔为2时,遍历完第一周,跳到第四周再遍历,以此类推。
代码如下:
1 public List<string> VolumeSet(VolumeSetDto input)View Code
2 {
3 GregorianCalendar gc = new GregorianCalendar();
4 var datesDictionary = new Dictionary<DateTime, DayOfWeek>();
5 var begin = input.beginTime.Date;
6 while (begin <= input.endTime.Date)
7 {
8 datesDictionary.Add(begin, gc.GetDayOfWeek(begin));
9 var week = gc.GetDayOfWeek(begin);
10 if (week == DayOfWeek.Saturday)
11 begin = begin.AddDays((7 * (input.interval ?? 0) + 1));
12 else begin = begin.AddDays(1);
13 }
14 var dates = new List<string>();
15 foreach (var date in datesDictionary)
16 {
17 if (input.dayWeek != null && input.dayWeek.Count != 0)
18 {
19 if (input.dayWeek.Contains(date.Value))
20 dates.Add(date.Key.ToString());
21 }
22 else dates.Add(date.Key.ToString());
23 }
24 return dates;
25 }
26
27 public class VolumeSetDto
28 {
29 /// <summary>
30 /// 开始时间
31 /// </summary>
32 public DateTime beginTime { get; set; }
33
34 /// <summary>
35 /// 结束时间
36 /// </summary>
37 public DateTime endTime { get; set; }
38
39 /// <summary>
40 /// 间隔周期
41 /// </summary>
42 public int? interval { get; set; }
43
44 /// <summary>
45 /// 选中周几把枚举Add进来
46 /// </summary>
47 public List<DayOfWeek> dayWeek { get; set; }
48 }