12

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 is in.

edit(2): This is how I get the week number:

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

I'm just trying to do the reverse.

Loris
  • 1,940
  • 3
  • 19
  • 27
  • 1
    The reverse is just not well-defined. The answers below contain some relevant questions, try to answer them. – H H Mar 18 '09 at 17:40

11 Answers11

10

If you assume that the first day of your definition of week is the same day as the 1st day of the year, then this will work:

int year = 2000;
int week = 9;
int month = new DateTime(year, 1, 1).AddDays(7 * (week - 1)).Month;

Obviously, a true answer would depend on how you define the first day of the week, and how you define how a week falls into a month when it overlaps more than one.

John Rasch
  • 62,489
  • 19
  • 106
  • 139
  • If we are talking about an ISO8601 week, then something based on the algorithms in http://stackoverflow.com/questions/662379/calculate-date-from-week-number would be truer. – Klas Mellbourn Mar 13 '13 at 06:01
  • Why would you assume that? You might as well assume that a week is 366 days long. You'd get it right one time out of 52 ;-) – Zano Nov 12 '13 at 15:44
  • I *wouldn't* assume that, I would specify the problem completely and then code something that conforms to that specification. Since that wasn't done in the question, I chose the simplest way to solve the underspecified problem that makes sense in a majority of cases. – John Rasch Nov 12 '13 at 16:49
4

This is what I ended up doing:

static int GetMonth(int Year, int Week)
{
    DateTime tDt = new DateTime(Year, 1, 1);

    tDt.AddDays((Week - 1) * 7);

    for (int i = 0; i <= 365; ++i)
    {
        int tWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            tDt, 
            CalendarWeekRule.FirstDay, 
            DayOfWeek.Monday);
        if (tWeek == Week)
            return tDt.Month;

        tDt = tDt.AddDays(1);
    }
    return 0;
}

I would have preferred something simpler, but it works :)

Loris
  • 1,940
  • 3
  • 19
  • 27
2

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

Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
2

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;
}
Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Janbert
  • 21
  • 1
1

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;
    }
    
1

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

Perchik
  • 5,262
  • 1
  • 19
  • 22
0

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.

johh
  • 141
  • 1
  • 5
0

You cant. You need at least the day on which the 1st week starts (or when the week starts), to get an accurate answer.

leppie
  • 115,091
  • 17
  • 196
  • 297
0

You cant. A week may start in one month and end in another.

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
0

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.

Jim H.
  • 5,539
  • 1
  • 24
  • 23
-1
// 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;
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
ravula sandeep
  • 511
  • 5
  • 9