Share a method for selecting dates in batches written by me and share the date Selection Method
The requirement is probably to select a period of time, select Sunday to Saturday, and return the date that meets the requirements. For example, if Sunday is selected, return all Sunday dates in the time period. If Sunday and Friday are selected, return all Friday and Sunday dates in the time period. The repetition interval is based on the day of the week in the cycle. When the interval is 0, every week is traversed. When the interval is 1, the first week is traversed, and then the traversal is skipped to the third week, when the interval is 2, traverse the first week, jump to the fourth week, then traverse, and so on.
The Code is as follows:
1 public List <string> VolumeSet (VolumeSetDto input) 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 VolumeSetDto28 {29 // <summary> 30 // start time 31 /// </summary> 32 public DateTime beginTime {get; set ;} 33 34 // <summary> 35 // end time 36 /// </summary> 37 public DateTime endTime {get; set ;} 38 39 // <summary> 40 // interval 41 // </sum Mary> 42 public int? Interval {get; set ;} 43 44 /// <summary> 45 // Add enumerations in weeks. 46 // </summary> 47 public List <DayOfWeek> dayWeek {get; set ;} 48}
View Code