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

后端 未结 8 1989
渐次进展
渐次进展 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:07

        private static DateTime FirstDateOfWeek(int year, int weekOfYear)
        {
            var firstDate = new DateTime(year, 1, 4);
            //first thursday of the week defines the first week (https://en.wikipedia.org/wiki/ISO_8601)
            //Wiki: the 4th of january is always in the first week
            while (firstDate.DayOfWeek != DayOfWeek.Monday)
                firstDate = firstDate.AddDays(-1);
    
            return firstDate.AddDays((weekOfYear - 1)*7);
        }
    

    Easyer and more efficient solution!

提交回复
热议问题