Share a method for selecting dates in batches written by me and share the date Selection Method

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.