Find the nearest Date, given a TargetDate and Day of Week 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.
Perl 6, 83 bytes
-&gt;$_,\w{~(Date.new(|m/(....)(..)(..)/)-3...*.day-of-week%7+1==w)[*-1]~~{S:g/&quot;-&quot;//}}

Try it
Expanded
-&gt;     # pointy block lambda
  $_,
  \w
{
  ~(  # turn into a Str

    Date.new( |m/(....)(..)(..)/ ) - 3 # three days earlier
    ...                                # generate a sequence
    *.day-of-week % 7 + 1 == w         # stop when the right day is found

  )[*-1]  # the last one

  ~~      # apply the following block

  {
    S:g/&quot;-&quot;//  # remove ｢-｣
  }
}