C# .NET 获取日期是第几周

/// <summary>
/// 获取日期是第几周
/// </summary>
/// <param name="date">日期</param>
/// <returns>第几周</returns>
public static int GetWeekOfYear(DateTime date)
{
	//确定此时间在一年中的位置
	int dayOfYear = date.DayOfYear;
	//当年第一天
	DateTime tempDate = new DateTime(date.Year, 1, 1);
	//确定当年第一天
	int tempDayOfWeek = (int)tempDate.DayOfWeek;
	tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
	//确定星期几
	int index = (int)date.DayOfWeek;
	index = index == 0 ? 7 : index;
	//当前周的范围
	DateTime retStartDay = date.AddDays(-(index - 1));
	DateTime retEndDay = date.AddDays(7 - index);
	//确定当前是第几周
	int weekOfYear = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
	if (retStartDay.Year < retEndDay.Year)
	{
		weekOfYear = 1;
	}
	return weekOfYear;
}
—— 完 ——
相关推荐
评论

立 为 非 似

中 谁 昨 此

宵 风 夜 星

。 露 , 辰

文章点击榜

细 无 轻 自

如 边 似 在

愁 丝 梦 飞

。 雨 , 花