Find the nearest Date to a TargetDate for a given Day of Week.
For example, given a date of 20161219
and a day of week of Friday (6)
, the answer is 20161216
.
Another example, given a date of 20161219
and a day of week of Wednesday (4)
, the answer is 20161221
.
A final example, given a date of 20161219
and a day of week of Monday (2)
, the answer is 20161219
.
Rules:
- The Date format for the input and output must match. In the examples, I used
yyyymmdd
, but you may use any format as long as the year (at least two digits), month, and day of month are "human readable". - Day of Week is input as an integer. In my example, Sunday is the first day of the week, therefore it is day of week number
1
. You can have any Day of Week numbering, as long as you note it when it differs from the example. - Years 1970 through 2030 must be accommodated.
- Common language date tools and libraries are allowed, but street cred is given to those that choose not to use them.
- Least number of bytes wins.