### Computing the Nth DoW of Any Year / Month

This algorithm handles the problem of computing the Nth DoW of a given year and month.  It was designed to help determine such things as the calendar dates of moving dates like Election Day and holidays (U.S.), like Thanksgiving Day.

For example, if a certain holiday was always observed on the third (N=3) Monday (DoW=1) of a certain month (m=Month number), then the following algorithm could be used to find the corresponding calendar date for any given year (y).

To compute the (`N`)th DoW of month (`m`) of year (`y`), requires 5 arguments:
1. `N` = Index (1, 2, 3, ...), as in the `N`th Thursday.

2. `DoW` = Day of Week index code (0 to 6)
Where:
`0`=Sun, `1`=Mon, `2`=Tue, `3`=Wed, `4`=Thu, `5`=Fri  and  `6`=Sat

3. `y` = Year value (Neg=BC)

4. `m` = Month number (`1` to `12`)

5. `CalMode` (`0`=Julian  or  `1`=Gregorian)

Given the above arguments and using Algorithm 1 and Algorithm 2, the following pseudocode algorithm computes the ISO integer-encoded date (`ISOymd`) value.

Algorithm 9:

Compute the corresponding ISO integer-encoded date (`ISOymd`) of the (`N`)th (`DoW`) in month (`m`) of year (`y`) and (`CalMode`).
``` if (y < 0) then YearSign = -1 else YearSign = 1

ISOymd1 = YearSign*(10000*abs(y) + 100*m + 1)
JD1 = JD_For_ISOymd (ISOymd1, CalMode)
DoW1 = (JD1 + 1) mod 7
dDoW = DoW - DoW1

if (dDoW < 0) then u = dDoW + 7 else u = dDoW

JD = JD1 + u + 7*(N-1)

ISOymd = ISOymd_For_JD (JD, CalMode)
```
or
``` ISOymd1 = ((y < 0)? -1:1)*(10000*abs(y) + 100*m + 1)
JD1 = JD_For_ISOymd (ISOymd1, CalMode)
DoW1 = (JD1 + 1) mod 7
dDoW = DoW - DoW1

JD = JD1 + ((dDoW < 0)? dDoW + 7 : dDoW) + 7*(N-1)

ISOymd = ISOymd_For_JD (JD, CalMode)
```

© PHP Science Labs - 2017