Viggen
Viggen

Reputation: 13

Loop within a date range for each month and each day

I have two text-boxes which are used to select a From and a To date. I need to have a loop where the outer loop will be for a year and the inner loop will run for each month.

Problem is with the code below, if I choose 11/01/2011 and 06/30/2012, my month loop runs once for month 11. After that the loop exits.. Any help is appreciated.

I'm using the code below to look into a SharePoint Calendar List (using CAML query) and fetch number of times 3, 5 consecutive days a certain room is available excluding week ends. Idea is to use CAML query to get the number of free days for each month and keep repeating till the last selected month.

int year = 0, month = 0; 
for (year = Calendar1.SelectedDate.Year; year <= Calendar2.SelectedDate.Year; year++)
{
  int i = year;
  for (month = Calendar1.SelectedDate.Month; month <= Calendar2.SelectedDate.Month; month++)
  {
    int j = month;
  }
}

Upvotes: 1

Views: 11360

Answers (6)

Friyank
Friyank

Reputation: 479

 private static void Main(string[] args)
        {
            Console.WriteLine(DateTime.DaysInMonth(2020, 1));

            var start = new DateTime(1900, 1, 1);
            var end = new DateTime(2000, 12, 31);

            end = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));
            var diff = Enumerable.Range(0, Int32.MaxValue)
                                 .Select(e => start.AddMonths(e))
                                 .TakeWhile(e => e <= end)
                                 .Select(e => e);
            foreach (var item in diff)
            {
                Console.WriteLine("Start Date - " + item.ToString("dd-MM-yyyy"));
                Console.WriteLine("End Date - " + item.AddDays(DateTime.DaysInMonth(item.Year, item.Month) - 1).ToString("dd-MM-yyyy"));
            }
        }

Upvotes: 0

Mohamad Mahmoud Darwish
Mohamad Mahmoud Darwish

Reputation: 4173

//Function return First day in Month For Date --example : 01-09-2012

public static  DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
     {
         return new DateTime(dateTime.Year, dateTime.Month, 1);

     }

//code used to loop throw a Date range for each month
DateTime FirstDayInMonth = FirstDayOfMonthFromDateTime(Date);
DateTime TempDay = FirstDayInMonth;
int days = DateTime.DaysInMonth(FirstDayInMonth.Year, FirstDayInMonth.Month);

for (int i = 0; i < days; i++)
   {
     System.Out.Println(TempDay.toString());
     TempDay.AddDays(1);
   }

//then used code for each month in year (simple loop from 1-12)..

Upvotes: 0

Mike
Mike

Reputation: 1067

For the start year, you need to start your inner loop from the appropriate month, and run through all 12 months, except on the end year, where you should run to the appropriate month. Something like this should work:

int year = 0, month = 0;    
for (year = Calendar1.SelectedDate.Year; year <= Calendar2.SelectedDate.Year; year++)
{
    int i = year;

    for (month = (i==Calendar1.SelectedDate.Year ? Calendar1.SelectedDate.Month : 1); month <= (i==Calendar2.SelectedDate.Year ? Calendar2.SelectedDate.Month : 12); month++)
    {
        int j = month;
    }
}

Upvotes: 0

BlackSpy
BlackSpy

Reputation: 5603

If I understand you correctly you want to iterate through each month between the 2 dates. If so, this should work:

var dt2 = Calendar2.SelectedDate.Year;
var current = Calendar1.SelectedDate;
while (current < dt2)
{
  current = current.AddMonths(1);
  //do your work for each month here
}

Upvotes: 4

Brandon Moore
Brandon Moore

Reputation: 8790

Your starting and ending number for your inner loop should be conditional.

If you're on the start year then the start month should be the selected month; otherwise it should be 1.

If you're on the end year then the end month should be the selected month; otherwise it should be 12.

Example:

var startYear = Calendar1.SelectedDate.Year;
var endYear = Calender2.SelectedDate.Year;
var startMonth = Calender1.SelectedDate.Month;
var endMonth = Calender2.SelectedDate.Month;

for (var year = startYear; year <= endYear; year++)
{
    var sm = year == startYear ? startMonth : 1;
    var em = year == endYear ? endMonth : 12;

    for (var month = sm; month <= em; month++)
    {

    }    
}

Upvotes: 1

Dave
Dave

Reputation: 81

Would something like this work?

for (DateTime date = Calendar1.SelectedDate; date < Calendar2.SelectedDate; date = date.AddMonths(1))
{
 //code
}

Upvotes: 7

Related Questions