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

后端 未结 11 2358
你的背包
你的背包 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 18:57

    In .NET 3.0 and later you can use the ISOWeek-Class.

    public static int MonthOfFirstDay(int year, int week)
    {
        return ISOWeek.ToDateTime(year, week, DayOfWeek.Monday).Month;
    }
    

    Note that the year might not fit, as the first week of a year can already start in end of December the year before. For instance the first week of 2020 started on Monday 2019-12-30.

提交回复
热议问题