# Formula to find the date given a specific year, month, and day of the week

I need a formula to get me to the first or third specified day of the week of the following year.

So, here is the information I am presented. I have a year (y), a month (m) and a day of the week (n). From those variables I need to find date the next year falls on.

For example lets say we start Wednesday 1/3/2018. I need a formula to get me to the first Wednesday of January 2019 which, looking at a calendar, is 1/2/2019. This needs to apply to all months and days of the week. A second example, say I start on Wednesday 2/7/2018. This formula has to get me to the first Wednesday of February 2019 which, again, looking at a calendar is 2/6/2019.

I have attempted to use Zeller's Rule and refactor to find the date but to no avail. I also found this link:

http://mathforum.org/library/drmath/view/55837.html

From here, I tried solving for d. But because of the truncation required I could not.

First, we move ahead by $52$ weeks, i.e., $364$ days to our given date $y$/$m$/$d$.
If this does not involve a leap day, this takes us to $y+1$/$m$/$d-1$. Otherwise, it takes us to $y+1$/$m$/$d-2$. To ensure that we are in the first week of the month, we play with remainders modulo $7$. Thus our target date is
• $y+1$/$m$/$(d+4\bmod 7)+1$ if $m\le 2$ and $y$ is a leap year
• $y+1$/$m$/$(d+4\bmod 7)+1$ if $m>2$ and $y+1$ is a leap year
• $y+1$/$m$/$(d+5\bmod 7)+1$ otherwise.