How do I get the month number from the year and week number in c#?

后端 未结 11 2355
你的背包
你的背包 2020-12-17 18:40

As the title says, given the year and the week number, how do I get the month number?

edit: if a week crosses two months, I want the month the first day of the week

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 19:03

    // Calculate the week number according to ISO 8601
    
        public static int Iso8601WeekNumber(DateTime dt)
        {
                return  CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        }
    
    // ...
    
    DateTime dt = DateTime.Now;
    
    // Calculate the WeekOfMonth according to ISO 8601
    int weekOfMonth = Iso8601WeekNumber(dt) - Iso8601WeekNumber(dt.AddDays(1 - dt.Day)) + 1;
    

提交回复
热议问题