Get date of first and last day of week knowing week number

后端 未结 8 2001
渐次进展
渐次进展 2020-12-16 20:24

I need to get the date of the first and last day of the week knowing the week number.

I get a start date and an end date, representing the first and last day of a s

8条回答
  •  情话喂你
    2020-12-16 21:06

    Tim Schmelter's method didn't work for Swedish (sv-SE) culture where weeks start on Mondays. This has been tested with en-US and sv-SE.

        public static DateTime GetFirstDateOfWeek(int year, int weekOfYear, CultureInfo cultureInfo)
        {
            DateTime jan1 = new DateTime(year, 1, 1);
            int daysOffset = (int)cultureInfo.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
            DateTime firstWeekDay = jan1.AddDays(daysOffset);
            int firstWeek = cultureInfo.Calendar.GetWeekOfYear(jan1, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
            if (firstWeek == 1)
            {
                weekOfYear -= 1;
            }
            return firstWeekDay.AddDays(weekOfYear * 7);
        }
    

提交回复
热议问题