#region 得到一周的周一和周日的日期 ////// 计算本周的周一日期 /// ///public static DateTime GetMondayDate() { return GetMondayDate(DateTime.Now); } /// /// 计算本周周日的日期 /// ///public static DateTime GetSundayDate() { return GetSundayDate(DateTime.Now); } /// /// 计算某日起始日期(礼拜一的日期) /// /// 该周中任意一天 ///返回礼拜一日期,后面的具体时、分、秒和传入值相等 public static DateTime GetMondayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } ////// 计算某日结束日期(礼拜日的日期) /// /// 该周中任意一天 ///返回礼拜日日期,后面的具体时、分、秒和传入值相等 public static DateTime GetSundayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); }#endregion
收集其它的方法/
DateTime dt = DateTime.Now; //当前时间DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一DateTime endWeek = startWeek.AddDays(6); //本周周日DateTime startMonth = dt.AddDays(1 - dt.Day); //本月月初DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); //本月月末//DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //本月月末DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //本季度初DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //本季度末DateTime startYear = new DateTime(dt.Year, 1, 1); //本年年初DateTime endYear = new DateTime(dt.Year, 12, 31); //本年年末
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoyz_1/archive/2009/01/15/3785705.aspx