none
Get the Fourth (4th) Friday of each month RRS feed

  • Question

  • I am trying to get the fourth (4th) Friday using the code below but it is giving me the last Friday. The fourth Friday of May for example  is 24th 2019.

    Below is my code. Any help will be appreciated

    var StartDate = DateTime.Parse("05/23/2019");
                var EndDate = DateTime.Parse("12/31/2019");
               
                var Monthly = 1;
                for (var i = StartDate; i < EndDate; i = i.AddMonths(Monthly))
                {
                    var MonthTarget = new DateTime(i.Year, i.Month,1).AddMinutes(1);
                    var targetFriday = Enumerable.Range(1,7)
                        .Select(dy => MonthTarget.AddDays(-dy))
                        .First(dy => dy.DayOfWeek == DayOfWeek.Friday);
    
                    Console.WriteLine(targetFriday);
    
                }


    Ebenezer

    Wednesday, April 24, 2019 10:15 PM

Answers

  • Hi denkyira,

    Here is a simple demo you can refer to.

        var StartDate = DateTime.Parse("05/23/2019");
        var EndDate = DateTime.Parse("12/31/2019");
    
        var Daily = 1;
        for (var i = StartDate; i < EndDate; i = i.AddMonths(Daily))
        {
            // Get the first of each month
            int month = i.Month;
            int year = i.Year;
            var FirstDay = new DateTime(year, month, 1);
            // Get the difference between first day and first Friday
            int difference = (int)DayOfWeek.Friday - (int)FirstDay.DayOfWeek;
            int adddays = difference >= 0 ? 21 : 28;
            int days = difference + adddays;
            var ForthFriday = FirstDay.AddDays(days);
            Console.WriteLine(ForthFriday.ToShortDateString());
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by denkyira Thursday, April 25, 2019 10:29 AM
    Thursday, April 25, 2019 2:38 AM
    Moderator

All replies

  • Hi denkyira,

    Here is a simple demo you can refer to.

        var StartDate = DateTime.Parse("05/23/2019");
        var EndDate = DateTime.Parse("12/31/2019");
    
        var Daily = 1;
        for (var i = StartDate; i < EndDate; i = i.AddMonths(Daily))
        {
            // Get the first of each month
            int month = i.Month;
            int year = i.Year;
            var FirstDay = new DateTime(year, month, 1);
            // Get the difference between first day and first Friday
            int difference = (int)DayOfWeek.Friday - (int)FirstDay.DayOfWeek;
            int adddays = difference >= 0 ? 21 : 28;
            int days = difference + adddays;
            var ForthFriday = FirstDay.AddDays(days);
            Console.WriteLine(ForthFriday.ToShortDateString());
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by denkyira Thursday, April 25, 2019 10:29 AM
    Thursday, April 25, 2019 2:38 AM
    Moderator
  • Thanks a lot. it works great.

    Ebenezer

    Thursday, April 25, 2019 10:29 AM