首页 > 代码库 > c# 获取一年中的周/根据一年中的第几周获取该周的开始日期与结束日期
c# 获取一年中的周/根据一年中的第几周获取该周的开始日期与结束日期
/// <summary> /// 获取一年中的周 /// </summary> /// <param name="dt">日期</param> /// <returns></returns> public static int GetWeekOfYear(DateTime dt) { System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar(); int weekOfYear = gc.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday); return weekOfYear; } /// <summary> /// 根据一年中的第几周获取该周的开始日期与结束日期 /// </summary> /// <param name="year"></param> /// <param name="weekNumber"></param> /// <param name="culture"></param> /// <returns></returns> public static Tuple<DateTime, DateTime> GetFirstEndDayOfWeek(int year, int weekNumber, System.Globalization.CultureInfo culture) { System.Globalization.Calendar calendar = culture.Calendar; DateTime firstOfYear = new DateTime(year, 1, 1, calendar); DateTime targetDay = calendar.AddWeeks(firstOfYear, weekNumber-1); DayOfWeek firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek; while (targetDay.DayOfWeek != firstDayOfWeek) { targetDay = targetDay.AddDays(-1); } return Tuple.Create<DateTime, DateTime>(targetDay, targetDay.AddDays(6)); }
c# 获取一年中的周/根据一年中的第几周获取该周的开始日期与结束日期
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。