标签:blog http io ar color os sp strong on
/// 取得某月的第一天
C# DateTime 日期加1天 减一天 加一月 减一月 等方法
/// <summary>根据日期,获得星期几</summary>
/// <param name="y">年</param>
/// <param name="m">月</param>
/// <param name="d">日</param>
/// <returns>星期几,1代表星期一;7代表星期日</returns>
public static int getWeekDay(int y, int m, int d)
{
if (m == 1) m = 13;
if (m == 2) m = 14;
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
return week;
}
/// <summary> /// 取指定日期是一年中的第几周 /// </summary> /// <param name="dtime">给定的日期</param> /// <returns>数字 一年中的第几周</returns> public static int WeekOfYear(DateTime dtime) { try { //确定此时间在一年中的位置 int dayOfYear = dtime.DayOfYear; //当年第一天 DateTime tempDate = new DateTime(dtime.Year, 1, 1); //确定当年第一天 int tempDayOfWeek = ( int )tempDate.DayOfWeek; tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek; ////确定星期几 int index = ( int )dtime.DayOfWeek; index = index == 0 ? 7 : index; //当前周的范围 DateTime retStartDay = dtime.AddDays(-(index - 1)); DateTime retEndDay = dtime.AddDays(6 - index); //确定当前是第几周 int weekIndex = ( int )Math.Ceiling((( double )dayOfYear + tempDayOfWeek - 1) / 7); if (retStartDay.Year < retEndDay.Year) { weekIndex = 1; } return weekIndex; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 求某年有多少周 /// </summary> /// <param name="dtime"></param> /// <returns></returns> public static int GetWeekOfYear(DateTime dtime) { int countDay = DateTime.Parse(dtime.Year + "-12-31" ).DayOfYear; int countWeek = countDay / 7; return countWeek; } //根据年月日获得星期几 public static string CaculateWeekDay( int month, int day) { string weekstr = string .Empty; int year = DateTime.Now.Year; //把一月和二月看成是上一年的十三月和十四月 if (month == 1) { month = 13; year--; } if (month == 2) { month = 14; year--; } int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; switch (week) { case 0: weekstr = "1" ; break ; case 1: weekstr = "2" ; break ; case 2: weekstr = "3" ; break ; case 3: weekstr = "4" ; break ; case 4: weekstr = "5" ; break ; case 5: weekstr = "6" ; break ; case 6: weekstr = "7" ; break ; } return weekstr; } /// <summary> /// 根据2个时间段获得相应的周数 /// </summary> /// <param name="beginDate"></param> /// <param name="endDate"></param> /// <returns></returns> public static int WeekOfDate(DateTime beginDate, DateTime endDate) { TimeSpan ts1 = new TimeSpan(beginDate.Ticks); TimeSpan ts2 = new TimeSpan(endDate.Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); int weeks = ts.Days / 7; //确定此时间在一年中的位置 int dayOfYear = beginDate.DayOfYear; //当年第一天 DateTime tempDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day); //最后一天 DateTime tempendDate = new DateTime(endDate.Year, endDate.Month, endDate.Day); int tempDayOfWeek = ( int )tempDate.DayOfWeek; tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek; ////确定星期几 int startindex = ( int )beginDate.DayOfWeek; startindex = startindex == 0 ? 7 : startindex; //当前周的范围 DateTime retStartDay = beginDate.AddDays(-(startindex - 1)); DateTime retEndDay = beginDate.AddDays(7 - startindex); //确定当前是第几周 int weekIndex = ( int )Math.Ceiling((( double )dayOfYear + tempDayOfWeek - 1) / 7); return weeks; } /// <summary> /// 根据起始时间,获取第几周 /// </summary> /// <param name="dtime">当前时间</param> /// <returns></returns> public static int WeekOfTermDate(DateTime dtime) { string datetime = "2011-3-1" ; TimeSpan ts1 = new TimeSpan(dtime.Ticks); TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime).Ticks); TimeSpan ts = ts2.Subtract(ts1).Duration(); //确定此时间在一年中的位置 int dayOfYear = ts.Days; //当年第一天 DateTime tempDate = new DateTime(Convert.ToDateTime(datetime).Year, Convert.ToDateTime(datetime).Month, Convert.ToDateTime(datetime).Day); int tempDayOfWeek = ( int )tempDate.DayOfWeek; tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek; ////确定星期几 int index = ( int )dtime.DayOfWeek; index = index == 0 ? 7 : index; //当前周的范围 DateTime retStartDay = dtime.AddDays(-(index - 1)); DateTime retEndDay = dtime.AddDays(7 - index); //确定当前是第几周 int weekIndex = ( int )Math.Ceiling((( double )dayOfYear + tempDayOfWeek) / 7); return weekIndex; } /// <summary> /// 根据周,星期获得具体年月日 /// </summary> /// <param name="week">第几周</param> /// <param name="day">星期几</param> /// <returns></returns> public static DateTime DateTimeByWeekAndDay( int week, int day) { DateTime someTime = Convert.ToDateTime( "2011-3-1" ); int i = someTime.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6; // i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); //获取第N周的星期一 someTime = someTime.Subtract(ts).AddDays((week - 1) * 7); //获得星期几 someTime = someTime.AddDays(day - 1); return someTime; } |
private static int GetWeekOfYear()
{
//一.找到第一周的最后一天(先获取1月1日是星期几,从而得知第一周周末是几)
int firstWeekend=7-Convert.ToInt32(DateTime.Parse(DateTime.Today.Year + "-1-1").DayOfWeek);
//二.获取今天是一年当中的第几天
int currentDay = DateTime.Today.DayOfYear;
//三.(今天 减去 第一周周末)/7 等于 距第一周有多少周 再加上第一周的1 就是今天是今年的第几周了
// 刚好考虑了惟一的特殊情况就是,今天刚好在第一周内,那么距第一周就是0 再加上第一周的1 最后还是1
return Convert.ToInt32(Math.Ceiling((currentDay - firstWeekend) / 7.0)) + 1;
}
C#获取本周、上周、本月、上月、本季度、上季度、本年、上一年起始时间和结束时间
标签:blog http io ar color os sp strong on
原文地址:http://www.cnblogs.com/keyyang/p/4137375.html