通过年月周查询查询本周有哪些天
2012-02-20 21:32
141 查看
这是今天做一个计划统计写的一个简单算法,通过年,月,周计算出本周有哪些天,测试效果图如下:
View Code
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; }
相关文章推荐
- 通过出生年月,分组查询人口年龄段人数
- Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
- 第92课作业,通过SerDes的方式对一下数据进行Hive的存储和查询操作
- 通过sql语句查询实现分页
- [SQL]查询数据库里都有哪些自己建的表
- kylin 通过jdbc驱动进行sql查询
- SQL 查询本周/本月/本季度/本年的数据
- 编写程序,用if else语句根据姓名查询teaching数据库中学生的基本信息和选课信息,学生姓名通过变量输入。不存在则输出提示信息
- Django查询一个权限中包含哪些用户
- dom4j通过xpath查询xml
- mysql 查询当天、本周,本月,上一个月的数据
- ThinkPHP按今天,本周,本月,本季度,本年,全部查询
- MySQL的YEARWEEK函数以及查询本周数据
- Mybatis通过拦截器实现分页查询
- --查询目前系统中都安装了哪些perl模块
- 查询linux服务器有哪些IP在连接
- 通过注册表查询Visual C++ Redistributable Package是否安装
- Java通过mongo-java-driver-3.0+查询mongodb数据库
- 通过查询系统表得到纵向的表结构