0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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.
$\endgroup$
  • $\begingroup$ How can I modify this to always accommodate a leap year? This formula needs to handle a project 'n' number of years into the future. $\endgroup$ – cain4355 Jan 26 '18 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.