C#获取周一、周日的日期 函数类
#region 得到一周的周一和周日的日期
/// <summary>
/// 计算本周的周一日期
/// </summary>
/// <returns></returns>
public static DateTime GetMondayDate()
{
return GetMondayDate(DateTime.Now);
}
/// <summary>
/// 计算本周周日的日期
/// </summary>
/// <returns></returns>
public static DateTime GetSundayDate()
{
return GetSundayDate(DateTime.Now);
}
/// <summary>
/// 计算某日起始日期(礼拜一的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
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);
}
/// <summary>
/// 计算某日结束日期(礼拜日的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
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