-1

I have written following method in order to return first Date of week by passing week number of year and year, I consider Monday as first day of week. I am wondering if there is any better way I can calculate this. Here is my code;

 public static DateTime GetFirstDateOfWeekByWeekNumber(int year, int weekNumber)
    {
        var date = new DateTime(year, 01, 01);
        var firstDayOfYear = date.DayOfWeek;
        var result = date.AddDays(weekNumber * 7);

        if (firstDayOfYear == DayOfWeek.Monday)
            return result.Date;
        if (firstDayOfYear == DayOfWeek.Tuesday)
            return result.AddDays(-1).Date;
        if (firstDayOfYear == DayOfWeek.Wednesday)
            return result.AddDays(-2).Date;
        if (firstDayOfYear == DayOfWeek.Thursday)
            return result.AddDays(-3).Date;
        if (firstDayOfYear == DayOfWeek.Friday)
            return result.AddDays(-4).Date;
        if (firstDayOfYear == DayOfWeek.Saturday)
            return result.AddDays(-5).Date;
        return result.AddDays(-6).Date;
    }
Learner
  • 776
  • 6
  • 14
  • 40
  • 1
    Get first monday of current week? – Ya Wang Nov 05 '15 at 21:13
  • It looks like @GSerg posted what you're looking for. If you don't do that, you should at least turn that into a `switch` instead of all the `if`s. – maxshuty Nov 05 '15 at 21:38
  • @MaxPoshusta How switch is better than if? As I remember, people suggest avoiding switch case. Can you give me any argument that tells switch is better than if's, thanks? – Learner Nov 05 '15 at 21:43
  • I don't know why you'd avoid `switch/case`. Just do whatever makes your code the most readable. In your case, it would be using `switch/case`. –  Nov 05 '15 at 21:44
  • 1
    Maybe what you're thinking about is using `switch/case` for polymorphism. That's bad design. See http://programmers.stackexchange.com/a/147227/20458 –  Nov 05 '15 at 21:46
  • @Learner It's been argued whether `switch` is faster or slower than `if`, at the end of the day the difference is negligible in most user cases. The real thing with the `switch` is that, in my opinion, its much more readable. – maxshuty Nov 05 '15 at 21:46
  • _I consider Monday as first day of week_ - the `DayOfWeek` enum starts with Sunday. – w.b Nov 06 '15 at 08:30

1 Answers1

0

If you want to replace that if/else construct, you can just calculate using the underlying enum values. The enum starts with Sunday = 0, Monday = 1, so in order to get your Monday-based values, you need to shift it around by one. You can do that in the following way:

int delta = -((int)firstDayOfYear + 6) % 7;

// and then just apply the delta
return result.AddDays(delta).Date

As a site note, don’t use multiple ifs with a mutally exclusive condition in sequence. In your example, if the week day was Monday, the construct would still check the value against every single condition there. But if you used a proper if/else if/else structure, then it would only do the first check and then jump to the end. Using a switch statement also avoids this as the branch to take is evaluated in a single step.

poke
  • 369,085
  • 72
  • 557
  • 602