您的位置:首页 > 其它

通过年月周查询查询本周有哪些天

2012-02-20 21:32 141 查看
这是今天做一个计划统计写的一个简单算法,通过年,月,周计算出本周有哪些天,测试效果图如下:

View Code

/// <summary>
/// 根据年月周计算时间
/// </summary>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <param name="week">周</param>
/// <returns>DateTime[]时间</returns>
public DateTime[] GetDaysByWeekAndMont(int year, int month, int week)
{
int firstDay = 0;
DateTime[] days = new DateTime[] { new DateTime() };
if (month < 13 && week < 6)
{
DateTime date = new DateTime(year, month, 1);
firstDay = GetFirstWeekDayWithMonth(date.DayOfWeek) + 7 * (week - 1);
if (firstDay <= GetLastDayWithMonth(year, month))
{
days = new DateTime[7];
for (int i = 0; i < 7; i++)
{
if (firstDay <= GetLastDayWithMonth(year, month) && month < 13)
days[i] = new DateTime(year, month, firstDay++);
else if (firstDay >= GetLastDayWithMonth(year, month) && month < 13)
{
firstDay = 1;
if (month != 12)
days[i] = new DateTime(year, ++month, firstDay++);
else
{
month = 1;
days[i] = new DateTime(++year, month++, firstDay++);
}
}
else if (firstDay <= GetLastDayWithMonth(year, month) && month > 12)
{
days[i] = new DateTime(year, month, firstDay++);
}
else if (firstDay >= GetLastDayWithMonth(year, month) && month > 12)
{
month = 1; firstDay = 1;
days[i] = new DateTime(year, month++, firstDay++);
}
}
}
else
throw new Exception(year.ToString() + "年" + month + "月没有第" + week + "周"); //Console.WriteLine("输入时间错误");
}
else if (month > 12 )
throw new Exception(year + "年没有" + month + "月");//Console.WriteLine("输入时间错误");
else if (month < 13 && week > 5)
throw new Exception(year + "年" + month + "月没有第" + week + "周");
return days;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: