我们在写程序的时候往往要计算出年,月,周的开始日期和结束日期,在这里给出统一求解方法
代码如下 | 复制代码 |
/// /// 声明期间类型枚举 /// publicenumPeriod {Day, Week, Month, Year}; /// /// 获取指定期间的起止日期 /// /// 期间类型 /// 开始日期 /// 结束日期 publicstaticvoidGetPeriod(Period period,outDateTime beginDate,outDateTime endDate) { intyear = DateTime.Today.Year; intmonth = DateTime.Today.Month; switch(period) { casePeriod.Year://年 beginDate =newDateTime(year, 1, 1); endDate =newDateTime(year, 12, 31); break; casePeriod.Month://月 beginDate =newDateTime(year, month, 1); endDate = beginDate.AddMonths(1).AddDays(-1); break; casePeriod.Week://周 intweek = (int)DateTime.Today.DayOfWeek; if(week == 0) week = 7;//周日 beginDate = DateTime.Today.AddDays(-(week - 1)); endDate = beginDate.AddDays(6); break; default://日 beginDate = DateTime.Today; endDate = DateTime.Today; break; } } |