日期:2014-05-18  浏览次数:20865 次

C#怎么实现按月循环
比如:2000-7到2005-5按月循环,日期默认从1号开始
请问怎样用C#实现,谢谢!

------解决方案--------------------
用AddMonths(1)递增到new DateTime(2005,5,1)时结束
------解决方案--------------------
DateTime _BeginDate =DateTime.Parse("2006-07-01");
DateTime _EndDate = DateTime.Parse("2005-05-01");

DateTime _Date =_BeginDate;
while (true)
{
MessageBox.Show(_Date.ToString("yyyy-MM"));
_Date = _Date.AddMonths(1);

if (_Date.Year >= _EndDate.Year && _Date.Month > _EndDate.Month) break;
}
------解决方案--------------------
AddDays -> AddMonth

C# code

            DateTime dateStart = new DateTime(2000, 01, 01);
            DateTime dateEnd = dateStart;
            do
            {
                dateEnd = dateEnd.AddMonths(1);
            } while (dateEnd.Year <= 2005 && dateEnd.Month <= 05);

------解决方案--------------------
C# code
 int year1 = Convert.ToDateTime(startdate).Year;
            int year2 = Convert.ToDateTime(enddate).Year;
            int month1 = Convert.ToDateTime(startdate).Month;
            int month2 = Convert.ToDateTime(enddate).Month;
            //计算总月份
            int month = (year2 - year1) * 12 + (month2 - month1);

------解决方案--------------------
public static int GetYearWeekCount(int strYear)
{
string returnStr = "";

System.DateTime fDt = DateTime.Parse(strYear.ToString() + "-01-01");
int k = Convert.ToInt32(fDt.DayOfWeek);
if (k == 1)
{
int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
int countWeek = countDay / 7 + 1;
return countWeek;
}
else
{
int countDay = fDt.AddYears(1).AddDays(-1).DayOfYear;
int countWeek = countDay / 7 + 2;
return countWeek;
}
}
循环添加月份