Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. DateTime StartDate = "3/10/2009";
  2. DateTime EndDate = "3/26/2009";
  3. int DayInterval = 3;
  4.  
  5. int count = 0;
  6.  
  7. for(int i = 0; i < n; i++)
  8. {
  9. count++;
  10. if(count >= DayInterval)
  11. {
  12. //take action
  13. count = 0;
  14. }
  15.  
  16. }
  17.  
  18. public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
  19. {
  20. for(var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
  21. yield return day;
  22. }
  23.  
  24. foreach (DateTime day in EachDay(StartDate, EndDate))
  25. // print it or whatever
  26.  
  27. foreach (DateTime date in StartDate.To(EndDate).ExcludeEnd()
  28. .Step(DayInterval.Days())
  29. {
  30. // Do something with the date
  31. }
  32.  
  33. DateTime StartDate = new DateTime(2009, 3, 10);
  34. DateTime EndDate = new DateTime(2009, 3, 26);
  35. int DayInterval = 3;
  36.  
  37. List<DateTime> dateList = new List<DateTime>();
  38. while (StartDate.AddDays(DayInterval) <= EndDate)
  39. {
  40. StartDate = StartDate.AddDays(DayInterval);
  41. dateList.Add(StartDate);
  42. }
  43.  
  44. public static IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
  45. {
  46. for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
  47. yield return day;
  48. }
  49.  
  50. public static IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
  51. {
  52. for (var month = from.Date; month.Date <= thru.Date || month.Month == thru.Month; month = month.AddMonths(1))
  53. yield return month;
  54. }
  55.  
  56. public static IEnumerable<DateTime> EachDayTo(this DateTime dateFrom, DateTime dateTo)
  57. {
  58. return EachDay(dateFrom, dateTo);
  59. }
  60.  
  61. public static IEnumerable<DateTime> EachMonthTo(this DateTime dateFrom, DateTime dateTo)
  62. {
  63. return EachMonth(dateFrom, dateTo);
  64. }
  65.  
  66. DateTime startDate = new DateTime(2009, 3, 10);
  67. DateTime stopDate = new DateTime(2009, 3, 26);
  68. int interval = 3;
  69.  
  70. for (DateTime dateTime=startDate;
  71. dateTime < stopDate;
  72. dateTime += TimeSpan.FromDays(interval))
  73. {
  74.  
  75. }
  76.  
  77. DateTime startDate = new DateTime(2009, 3, 10);
  78. DateTime stopDate = new DateTime(2009, 3, 26);
  79. int interval = 3;
  80.  
  81. while ((startDate = startDate.AddDays(interval)) <= stopDate)
  82. {
  83. // do your thing
  84. }
  85.  
  86. var today = DateTime.UtcNow;
  87. var birthday = new DateTime(2018, 01, 01);
  88.  
  89. var toBirthday = today.RangeTo(birthday);
  90.  
  91. var toBirthday = today.RangeTo(birthday, x => x.AddMonths(2));
  92.  
  93. var toBirthday = today.RangeTo(birthday, x => x.AddYears(1));
  94.  
  95. // same result
  96. var fromToday = birthday.RangeFrom(today);
  97. var toBirthday = today.RangeTo(birthday);
  98.  
  99. public static DateTimeExtensions
  100. {
  101.  
  102. public static IEnumerable<DateTime> RangeTo(this DateTime from, DateTime to, Func<DateTime, DateTime> step = null)
  103. {
  104. if (step == null)
  105. {
  106. step = x => x.AddDays(1);
  107. }
  108.  
  109. while (from < to)
  110. {
  111. yield return from;
  112. from = step(from);
  113. }
  114. }
  115.  
  116. public static IEnumerable<DateTime> RangeFrom(this DateTime to, DateTime from, Func<DateTime, DateTime> step = null)
  117. {
  118. return from.RangeTo(to, step);
  119. }
  120. }
  121.  
  122. // looping between date range
  123. while (startDate <= endDate)
  124. {
  125. //here will be your code block...
  126.  
  127. startDate = startDate.AddDays(1);
  128. }
  129.  
  130. while (startdate <= enddate)
  131. {
  132. // do something with the startdate
  133. startdate = startdate.adddays(interval);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement