How to Use JavaScript to Determine the First Monday and Other Nth Weekdays of the Month

How to Use JavaScript to Determine the First Monday and Other Nth Weekdays of the Month

 

Developing applications often requires the ability to pinpoint specific weekdays within a given month. Whether you’re trying to find the third Wednesday, the second Tuesday, or specifically the first Monday of the month.
In this guide, we will explore how to ‘Determine Nth Weekday Month‘ using JavaScript. Such insights are crucial for apps requiring specific scheduling capabilities.

How to “Determine Nth Weekday Month”: Basic Steps!

Let’s dive into an example to understand better: Determining the second Tuesday of September 2021.


    With({
        MonthStart: Date(2021,9,1),
        WeekdayNum: 3,
        Instance: 2
    },
    DateAdd(MonthStart,
            (Instance * 7) - Weekday(DateAdd(MonthStart,7-WeekdayNum))
    )
    ) 
    

Reference:

  1. With function in Power Apps
  2. DateAdd, DateDiff, and TimeZoneOffset functions in Power Apps

Decoding the Approach

The strategy begins with the first day of the target month. For instance, if you’re keen on identifying the Nth Tuesday, the primary step involves calculating a start date positioned N weeks from the onset of the month:


    DateAdd(MonthStart,
        (Instance * 7)
    )
    

Referring back to our example of the second Tuesday in September 2021, this portion of the formula returns 15th September 2021. To align with our objective, we backtrack a day, landing on 14th September 2021. The segment that facilitates this subtraction is:


    Weekday(DateAdd(MonthStart,7-WeekdayNum))
    

This adjustment adds 4 days (7-3) to the month’s commencement, pointing to 5th September, a Sunday. Given that Sunday’s value is 1, our formula successfully adds 13 days to the 1st of September, resulting in 14th September.

Fetching the First Four Weekdays of Any Month

Building on the previous logic, we can devise a method that offers the first through fourth occurrences of weekdays for any month. The formula for September 2021 unfolds as:


    With({MonthStart: Date(2021,9,1)},
        ForAll(Sequence(7),
            {
                DayLabel: Switch(Value,
                                1,"Sun",
                                2,"Mon",
                                3,"Tue",
                                4,"Wed",
                                5,"Thu",
                                6,"Fri",
                                7,"Sat"
                ),
                First: DateAdd(MonthStart,
                    (1 * 7) - Weekday(DateAdd(MonthStart,7-Value))
                ),
                Second: DateAdd(MonthStart,
                    (2 * 7) - Weekday(DateAdd(MonthStart,7-Value))
                ),
                Third: DateAdd(MonthStart,
                    (3 * 7) - Weekday(DateAdd(MonthStart,7-Value))
                ),
                Fourth: DateAdd(MonthStart,
                    (4 * 7) - Weekday(DateAdd(MonthStart,7-Value))
                )
            }
        )
    )
    

Reference: ForAll function in Power Apps

In summary, for developers and software architects, being able to pinpoint specific weekdays in a month is often essential. Whether it’s identifying the 2nd Tuesday, the 3rd Wednesday, or especially the first Monday, the formulas and methods presented here provide a solid foundation.

If you face any obstacles while utilizing this method or require advanced technical guidance, don’t hesitate to contact us. We’re dedicated to assisting you and offer specialized solutions tailored to your distinct needs.


If you want to learn more about the Power Apps, feel free to explore our other informative articles and tutorials.

About The Author