1

I am a little confused about how to check if a given date day is the day that appears the fifth time in a month. For example, the below are some days of week that appears 5 times in a month:

  • Thursday 29th March 2018
  • Friday 30th March 2018
  • Saturday 31st March 2018
  • Saturday 29th April 2018

I want a method that will return true or false if a given date day is the fifth week day of a month.

I have tried the below code from this question but this method only returns the first date that starts the fifth week cycle.

 public DateTime? FifthDay(DayOfWeek dayOfWeek, byte monthNum, int year)
 {
      var searchDate = new DateTime(year, monthNum, 1);
      var weekDay = searchDate.DayOfWeek;

      if (weekDay == dayOfWeek)
      {
          searchDate = searchDate.AddDays(28);
      }

      for (DateTime d = searchDate; d < d.AddDays(7); d = d.AddDays(1))
      {
          if (d.DayOfWeek == dayOfWeek)
          {
              searchDate = searchDate.AddDays(28);
              break;
          }
       }

       if (searchDate.Month == monthNum)
          return searchDate;

      return null;
  }

Thank you

Big Smile
  • 1,103
  • 16
  • 30
  • You era now dealing with Dates, Timeszones and internationalsiation now. American and Brits can not even agree at wich **day** the week starts on, wich makes this whole question a tangeled mess. My only advice? Most of the functions relating to dates do use the current Windows Format selection. Wich might include that data. Aside from that ideal case I would jsut never touch those thematics: https://www.youtube.com/watch?v=-5wpm-gesOY https://www.youtube.com/watch?v=0j74jcxSunY – Christopher Feb 05 '18 at 23:23
  • I would probably try the code from the upvoted, accepted answer from the question you linked? If you are unfamiliar with linq, but answer works, you could probably request help writing that code using loops. – zzxyz Feb 05 '18 at 23:24
  • 1
    Possible duplicate of [Get the correct week number of a given date](https://stackoverflow.com/questions/11154673/get-the-correct-week-number-of-a-given-date) – Craig W. Feb 05 '18 at 23:25
  • @Christopher It doesn't matter on what day a week begins. The OP just wants to know if the day of the week occurs 5 times in the same month. – juharr Feb 05 '18 at 23:40
  • @Christopher This is about days in the month, not the week. If the day portion is 29, 30, or 31, then it's the fifth occurrence of that day (unless there is some calendar in which there are more or less than 7 days in a week) – Rufus L Feb 05 '18 at 23:56
  • To clarify - are you asking if the date itself is the fifth occurrence of a weekday? If so the "date > 28" answer nails it. Or are you attempting to determine which days of the week occur five times in a month so that you can tell if a given date is a weekday that occurs a total of five times in that month? – Scott Hannen Feb 06 '18 at 00:30

1 Answers1

5

Isn't any date over 28 going to be the fifth occurrence of that week day?

public static bool IsFifthWeek(DateTime d) => d.Day > 28;
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • Yes. The 29th, 30th and 31st are the only (and are always) the fifth occurences of a DayOfWeek in a month. (anything greater than 7 * 4) – Rufus L Feb 05 '18 at 23:54