1

我有两个文本框,用于选择 From 和 To 日期。我需要有一个循环,其中外循环将持续一年,而内循环将每个月运行。

问题出在下面的代码上,如果我选择 11/01/2011 和 06/30/2012,我的月循环在第 11 个月运行一次。之后循环退出.. 任何帮助表示赞赏。

我正在使用下面的代码查看 SharePoint 日历列表(使用 CAML 查询)并连续 3、5 天获取某个房间可用的次数,不包括周末。想法是使用 CAML 查询来获取每个月的空闲天数,并一直重复到最后一个选定的月份。

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;
  }
}
4

6 回答 6

6

像这样的东西会起作用吗?

for (DateTime date = Calendar1.SelectedDate; date < Calendar2.SelectedDate; date = date.AddMonths(1))
{
 //code
}
于 2012-06-13T21:59:41.267 回答
4

如果我对您的理解正确,您想在两个日期之间每个月进行迭代。如果是这样,这应该工作:

var dt2 = Calendar2.SelectedDate.Year;
var current = Calendar1.SelectedDate;
while (current < dt2)
{
  current = current.AddMonths(1);
  //do your work for each month here
}
于 2012-06-13T22:07:05.490 回答
1

您的内部循环的开始和结束编号应该是有条件的。

如果您在开始年,那么开始月份应该是选定的月份;否则它应该是1。

如果您在年末,那么末月应该是选定的月份;否则应该是12。

例子:

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++)
    {

    }    
}
于 2012-06-13T21:56:27.263 回答
0

对于开始年,您需要从适当的月份开始您的内部循环,并运行所有 12 个月,除了结束年,您应该运行到适当的月份。像这样的东西应该工作:

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;
    }
}
于 2012-06-13T21:58:58.083 回答
0
//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)..
于 2012-09-26T09:41:45.660 回答
0
 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"));
            }
        }
于 2021-09-23T16:21:04.310 回答