Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DateTime StartDate = "3/10/2009";
- DateTime EndDate = "3/26/2009";
- int DayInterval = 3;
- int count = 0;
- for(int i = 0; i < n; i++)
- {
- count++;
- if(count >= DayInterval)
- {
- //take action
- count = 0;
- }
- }
- public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
- {
- for(var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
- yield return day;
- }
- foreach (DateTime day in EachDay(StartDate, EndDate))
- // print it or whatever
- foreach (DateTime date in StartDate.To(EndDate).ExcludeEnd()
- .Step(DayInterval.Days())
- {
- // Do something with the date
- }
- DateTime StartDate = new DateTime(2009, 3, 10);
- DateTime EndDate = new DateTime(2009, 3, 26);
- int DayInterval = 3;
- List<DateTime> dateList = new List<DateTime>();
- while (StartDate.AddDays(DayInterval) <= EndDate)
- {
- StartDate = StartDate.AddDays(DayInterval);
- dateList.Add(StartDate);
- }
- public static IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
- {
- for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
- yield return day;
- }
- public static IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
- {
- for (var month = from.Date; month.Date <= thru.Date || month.Month == thru.Month; month = month.AddMonths(1))
- yield return month;
- }
- public static IEnumerable<DateTime> EachDayTo(this DateTime dateFrom, DateTime dateTo)
- {
- return EachDay(dateFrom, dateTo);
- }
- public static IEnumerable<DateTime> EachMonthTo(this DateTime dateFrom, DateTime dateTo)
- {
- return EachMonth(dateFrom, dateTo);
- }
- DateTime startDate = new DateTime(2009, 3, 10);
- DateTime stopDate = new DateTime(2009, 3, 26);
- int interval = 3;
- for (DateTime dateTime=startDate;
- dateTime < stopDate;
- dateTime += TimeSpan.FromDays(interval))
- {
- }
- DateTime startDate = new DateTime(2009, 3, 10);
- DateTime stopDate = new DateTime(2009, 3, 26);
- int interval = 3;
- while ((startDate = startDate.AddDays(interval)) <= stopDate)
- {
- // do your thing
- }
- var today = DateTime.UtcNow;
- var birthday = new DateTime(2018, 01, 01);
- var toBirthday = today.RangeTo(birthday);
- var toBirthday = today.RangeTo(birthday, x => x.AddMonths(2));
- var toBirthday = today.RangeTo(birthday, x => x.AddYears(1));
- // same result
- var fromToday = birthday.RangeFrom(today);
- var toBirthday = today.RangeTo(birthday);
- public static DateTimeExtensions
- {
- public static IEnumerable<DateTime> RangeTo(this DateTime from, DateTime to, Func<DateTime, DateTime> step = null)
- {
- if (step == null)
- {
- step = x => x.AddDays(1);
- }
- while (from < to)
- {
- yield return from;
- from = step(from);
- }
- }
- public static IEnumerable<DateTime> RangeFrom(this DateTime to, DateTime from, Func<DateTime, DateTime> step = null)
- {
- return from.RangeTo(to, step);
- }
- }
- // looping between date range
- while (startDate <= endDate)
- {
- //here will be your code block...
- startDate = startDate.AddDays(1);
- }
- while (startdate <= enddate)
- {
- // do something with the startdate
- startdate = startdate.adddays(interval);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement