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

后端 未结 11 2301
你的背包
你的背包 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:44

    Wouldn't it also depend on the day of the week?

    0 讨论(0)
  • 2020-12-17 18:44

    Assumptions:

    1. Sunday is the first day of the week.
    2. Partial week still counts as week 1
    3. Outputs beginning and ending month as integer array.

      public int[] getMonth(int weekNum, int year)
      {
      
          DateTime StartYear = new DateTime(year, 1, 1);
          System.DayOfWeek StartDOW = StartYear.DayOfWeek;
          DateTime DayOfYearWeekStart = default(DateTime);
          DateTime DayOfYearWeekEnd = default(DateTime);
          int x = 0;
          if ((StartDOW == System.DayOfWeek.Sunday)) {
              DayOfYearWeekStart = StartYear.AddDays((weekNum - 1) * 7);
              DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
          } else {
              for (x = 0; x <= 7; x += 1) {
                  if (StartYear.AddDays(x).DayOfWeek == DayOfWeek.Sunday) {
                      break; // TODO: might not be correct. Was : Exit For
                  }
              }
      
              if (weekNum == 1) {
                  DayOfYearWeekStart = StartYear;
                  DayOfYearWeekEnd = StartYear.AddDays(x - 1);
              } else if (weekNum > 1) {
                  DayOfYearWeekStart = StartYear.AddDays(((weekNum - 2) * 7) + x);
                  DayOfYearWeekEnd = DayOfYearWeekStart.AddDays(6);
              }
      
          }
      
          int[] Month = new int[2];
          Month[0] = DayOfYearWeekStart.Month;
          Month[1] = DayOfYearWeekEnd.Month;
      
          return Month;
      }
      
    0 讨论(0)
  • 2020-12-17 18:48

    this should be able to help

     public int getMonth(int weekNum, int year)
     {
         DateTime Current = new DateTime(year, 1, 1);
         System.DayOfWeek StartDOW = Current.DayOfWeek;
         int DayOfYear = (weekNum * 7) - 6; //1st day of the week
    
         if (StartDOW != System.DayOfWeek.Sunday) //means that last week of last year's month
         {
             Current = Current.AddDays(7 - (int)Current.DayOfWeek);
         }
         return Current.AddDays(DayOfYear).Month;
    }
    
    0 讨论(0)
  • 2020-12-17 18:48

    I think you're assuming that a "week" is any group of 7 sequential days. It isn't. Given Year(2008), Week(5), you could be in either January or Febuary, depending on when your "week" starts.

    0 讨论(0)
  • 2020-12-17 18:52

    Another problem you could face is that most years do not start at the beginning of a week, which shifts everything.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题