1

I would like to calculte the number of the week in a year. I see this post

In this post the acepted answer use this code:

public static int GetIso8601WeekOfYear(DateTime time)
{
    // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll 
    // be the same week# as whatever Thursday, Friday or Saturday are,
    // and we always get those right
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    // Return the week of our adjusted day
    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}

However I see a problem. The number of the week is repeated if the last day of a month is in Sunday.

For example, the last day of March of 2013 year is in sunday. This week is the number 13th, is correct. But in April, how C# use always 6 weeks in a month to calculate the number of week, the first week of april has not any day of april, because all the days belong to march because the last day of the week is 30th March. So C# says that the first week of april is th 15th week, but this is incorrect, it has to be 14th.

So I would like to know if there are any way to calculate the number of a week in a right way.

EDIT:

I mean this:

In march, the last week is this:

25 26 27 28 29 30 31

This is the 13th, is correct.

In april, the first week is:

1 2 3 4 5 6 7

And this week is calculated as 15th.

So if I see the march calendar the last week is calculated as 13th and if I see the april calendar the last week of march is caluclated as 14th. This is incorrect.

SOLUTION:

DateTime dtCalendar = Calendar.DisplayDate;
int gridRow = (int)GetValue(Grid.RowProperty);

// Return the week of our adjusted day
int wueekNumber= System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(dtCalendar, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);

if (dtCalendar.DayOfWeek == DayOfWeek.Monday)
{
    gridRow = gridRow - 1;
}

Text = (weekNumbe r+ gridRow - 1).ToString();  

Thanks.

Community
  • 1
  • 1
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

1

The problem is that you are using the wrong CalendarWeekRule. To get the result you want you should use FirstDay. I have seen various codes in internet saying that you should use FirstFourDayWeek but, after some testing, I realised that the "right one" is FirstDay. I have tested it with your example and it delivers the right result: 14th week.

int targetYear = 2013;
DateTime targetDate = new DateTime(targetYear, 4, 1);
int week = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(targetDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • I try to use the rule FirstDay, but the first week of april is 15th, and it has to be 14th. Which code do you use? – Álvaro García Jul 17 '13 at 18:47
  • Well, what I am doing really is modify a calender to show the woeek number. So I know that a month shows 6 weeks always, so when I get the number week of the displayDay I can sum the row of the calendar. But this does not work when the previous month has its last day in Monday. – Álvaro García Jul 17 '13 at 18:50
  • 1
    But why you do all this? Why not using this function to get automatically the week number. I have tested it and works fine. – varocarbas Jul 17 '13 at 18:51
  • 1
    The reason why you are not getting the right week even despite of using the right week-rule is because you are over-modifying the dates and are provoking a cumulative error. Keep it simple and it would work fine. – varocarbas Jul 17 '13 at 18:53
  • 1
    Just to make this point completely clear: evidently, there is no right/wrong rule. But the one working in our (European) calendars, without any further modification is "FirstDay". You had to edit the input date because the "FirstFourDayWeek" is not so regular but you don't need to do this anymore: you can rely on my code without any further modification. – varocarbas Jul 17 '13 at 19:00
  • Yes, that works. The problem is when I try to show the result in the calendar, because I don't have in count the fist row that is a row of the precious month and the actual month has not days. – Álvaro García Jul 17 '13 at 19:01
  • 1
    I guess that for this you would have to create a custom algorithm analysing the given year. As a first idea, I guess that you should retrieve all the weeks for the current month and the previous one and create a function understanding these results. For example, in this case, you should know which day is the last one in the previous week (13th) via iteration backwards with the final days of the previous month. But I don't think that there is an in-built functionality for that; nonetheless, does not sound too difficult to be built. – varocarbas Jul 17 '13 at 19:07
  • 1
    I get it. I only need to check if the displayDate (the first day of the month) is on Monday. If this is true, I sum n -1. If not, I sum n. I have updated my first post with the solution. Thanks. – Álvaro García Jul 17 '13 at 19:10
  • For example. I proposed to rely on the last week number, but actually your approach is quicker. You are welcome. – varocarbas Jul 17 '13 at 19:13