2

i am getting week no from database need to calculate which week of the month it is.

for ex:

week no = 7 month = 2

week no 11 month = 3

How to calculate it??

Just to add on i also have the year value with the week no.

ankur
  • 4,565
  • 14
  • 64
  • 100

6 Answers6

4

Ok, I'll have a go, but it's not very pretty:

public int MonthOfWeek( int week, int year, CalendarWeekRule, weekrule, DayOfWeek firstDayOfWeek)
{
  GregorianCalendar gc = new GregorianCalendar();
  for( DateTime dt = new DateTime(year, 1, 1); dt.Year == year; dt = dt.AddDays(1))
  {
    if( gc.GetWeekOfYear( dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == week ){
      return dt.Month;
    }
  }
  return -1;
}  

This takes into consideration the culture specific rules for generating weeks. If a week is split between two months it will return the first month it encounters.

So in Norway, I would call this with CalendarWeekRule.FirstFourDayWeek and DayOfWeek.Monday for correct results.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
2

Assuming that you are looking for the month in the given year:

var dtYearStart = new DateTime(DateTime.Now.Year, 1, 1);
CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;

// Check for the starting date of the initial week.
int diff = dtYearStart.DayOfWeek - defaultCultureInfo.DateTimeFormat.FirstDayOfWeek;
if (diff < 0)
{
 diff += 7;
}

dtYearStart = dtYearStart.AddDays(-1 * diff).Date;

int weekNo = 15;

DateTime dtWeekBased = dtYearStart.AddDays((weekNo-1) * 7);
string strMonth  = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dtWeekBased.Month);
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
  • 2
    I think you should add `(weekNo-1)*7`, am I wrong? Anyway, +1 because it's a good answer! :) – Marco May 10 '11 at 09:56
  • Does not work - the first calendar week of a year may start in December of the previous year. – Thorsten Dittmar May 10 '11 at 09:58
  • @Marco: I just checked, yes it should be weekNo-1, thanks. I've updated. – Brian Scott May 10 '11 at 10:03
  • You are not sure that the first week will be the first 7 days of the year. This will not always give the correct result. – Øyvind Bråthen May 10 '11 at 10:05
  • @Thortsten, if the week starts in the previous year it shouldn't matter as adding on additional weeks will always push the date into a subsequent relative date for the correct week index. – Brian Scott May 10 '11 at 10:11
  • @Thorsten, actually, I see the problem now. The month is the actual start date of the week. Ok, I'll ammend shortly. – Brian Scott May 10 '11 at 10:12
  • @Brian Scott - Will work some places I guess. This week thing is highly culture specific. In Norway, the first week of the year is the first week that has at least 4 days, and the first day of week is Monday. So if 29.12.2010 was a Monday, that Monday belongs to week 1 of 2011, but if it was on 28.12.2010 it belongs to week 52/53 of 2010. – Øyvind Bråthen May 10 '11 at 10:19
  • @Thorston, I've updated the initial day of the week type for different cultures. – Brian Scott May 10 '11 at 10:22
2

Calculate a date from weeknumber:

Calculate date from week number

Get the WeekOfMonth:

Calculate week of month in .NET

Community
  • 1
  • 1
Till
  • 3,084
  • 17
  • 18
0

Using DateTime, you can multiply week number by 7, get the total days passed on the year, add those days to 31/12/yyyy-1 and the get the month from the resulting DateTime.

I hope this to be useful to you.

See you.

Amedio
  • 895
  • 6
  • 13
  • Does not work - the first calendar week of a year may start in December of the previous year. – Thorsten Dittmar May 10 '11 at 09:58
  • I understand, you have to set then 31/12/yyyy-1? Sorry for the mistake, I will edit now – Amedio May 10 '11 at 09:59
  • @Amedio - How the weeks is generated is dependent on culture specific settings also. This will not always provide the correct results unfortunately. – Øyvind Bråthen May 10 '11 at 10:07
  • @Oyvind - Then my solution would only work correctly where the weeks long 7 days and the years long 365/366 days? – Amedio May 10 '11 at 10:12
  • @Amedio - What does it help subtracting one day from 31.12.yyyy? The first day of week 1 can (in Norway) be either day from 28.12.yyyy-1 to 3.1.yyyy. It depends on which day of week the new year starts on. – Øyvind Bråthen May 10 '11 at 10:21
  • @Oyvind - Ooook, I understand now, if I just multiply with 7, I will became in an error, if my year doesn't start in Monday/Sunday (Depending of the culture) I'm correct? – Amedio May 10 '11 at 10:25
0

4 week = 1 month so use

week%4

its return you no of month

Bhargav Mistri
  • 944
  • 8
  • 20
  • 4 weeks != 1 month. 4 weeks = 4*7 days = 28 days. Only February qualify for this (at least most years). This is as inaccurate as you can get it. – Øyvind Bråthen May 10 '11 at 10:35
  • but he want only month from a week not days from a week – Bhargav Mistri May 11 '11 at 11:02
  • First of, I guess you mean week/4 (also division, and not modulus). It still wont work. An example: Week 14 is 4.-10. April. Yet, 14/4 = 3.5 indicating March. If you are thinking that January is 0 and not 1, then it gets correct for week 14, since 3 is April, but for week 51 for example (19.-25. December) you will get 51/4 = 12.75 indicating that week 51 belongs to the 13th month of the year. Doesn't give much sense. Since a month is **not** 4 weeks, you can't simply divide by 4 to get the correct result. – Øyvind Bråthen May 11 '11 at 11:59
0

Here a solution using the Time Period Library for .NET:

// ----------------------------------------------------------------------
public int GetMonthOfWeek( int weekOfYear )
{
  return new Week( weekOfYear ).FirstDayStart.Month;
} // GetMonthOfWeek