When is the closest Conway's Doomsday? A quote from MO.SE answer:


  Although it is well known that Conway was able to quickly calculate the day of the week of any given date, it is less well known that one part of the algorithm is easy to remember and useful in practice: In any given year, the following dates all fall on the same day of the week: 4/4, 6/6, 8/8, 10/10, 12/12, 5/9, 9/5, 7/11, 11/7, and the last day of February. For example, in 2020, all these dates fall on a Saturday. Conway, in his characteristically colorful way, would say that the Doomsday of 2020 is Saturday. Knowing this fact allows you to calculate fairly quickly in your head, with no special training, the day of the week for any date in 2020.


Well, it sounds easy, but then we need to check which doomsday is the closest from the given date in order to quickly calculate the day of week. Now that sounds hard.

Practically, we'd just compare a given date with the Doomsday in the same month (or adjacent month in case of January and March).

Task

Given a date consisting of full year, month, and day, output the closest Conway's Doomsday (i.e. one of 4/4, 6/6, 8/8, 10/10, 12/12, 5/9, 9/5, 7/11, 11/7, and the last day of February) from the given date.

The closest Doomsday can be in the same month, a different month, or even a different year. If the given date has two nearest Doomsdays, output any one or both of them. Also note that the last day of February can be either 28th or 29th (depending on leap-year-ness).

You can take input and produce output in any suitable format, e.g. three integers, a formatted string, or even a built-in Date object (if your language has one). You can assume the given date is valid, and the input year is between 1901 and 2099 inclusive. Gregorian calendar is assumed in this challenge.

Test cases

YYYY-MM-DD =&gt; YYYY-MM-DD, ...
-------------------------------
2020-05-18 =&gt; 2020-05-09
2020-05-30 =&gt; 2020-06-06
2020-10-31 =&gt; 2020-11-07
2020-10-24 =&gt; 2020-10-10 or 2020-11-07
2020-01-20 =&gt; 2019-12-12
2020-01-21 =&gt; 2020-02-29
2019-01-20 =&gt; 2018-12-12 or 2019-02-28


Reference implementation in Python.
Charcoal, 138 110 98 bytes

≔§⪪”)¶⊟eΦＯ∨ü&amp;-T[¿Ｑ№i⧴⊕%⁰q”⁴ＬΦ⪪”)¶ »Ｒ≦ＰH↘{⎚″4χχβ´ΣP”⁴›ι✂θ⁴χ¹η¿⁼Ｉη¹²⁺⊖…θ⁴1212«…θ⁴¿⁻Ση³η«022§9888Ｉ…θ⁴


Try it online! Link is to verbose version of code. Edit: Saved 28 bytes by switching I/O to compact ISO format (yyyymmdd). Saved 12 bytes when the supported year range was restricted to 1901 to 2099. Explanation:

≔§⪪”)¶⊟eΦＯ∨ü&amp;-T[¿Ｑ№i⧴⊕%⁰q”⁴ＬΦ⪪”)¶ »Ｒ≦ＰH↘{⎚″4χχβ´ΣP”⁴›ι✂θ⁴χ¹η


Take the dates (in mmdd format) 0120, 0317, 0421, 0523, 0623, 0725, 0822, 0922, 1024, 1124. These represent the cutoff points above which the next Doomsday is nearer. (In some cases the Doomsday is equidistant but in particular for 0120 that is not true on leap years.) Work out which cutoff point applies by counting the number of dates that fall before the input date. Then look up the relevant Doomsday date from the list (in mmdd format) 1212, 1107, 1010, 0905, 0808, 0711, 0606, 0509, 0404, 0300, 0012.

If I had access to a date library I could then ask it to fix up my date, but unfortunately I have to do that manually:

¿⁼Ｉη¹²


Is this the 0012 date, meaning the 12th of month 0, i.e. last December?

⁺⊖…θ⁴1212«


If so then output the previous year and a month and day of 12.

…θ⁴


Otherwise the year is at least correct...

¿⁻Ση³


Is this the 0300 date, meaning the 0th of March, i.e. the last day of February?

η«


If not then this is the date we seek.

022


Output February the 2?th.

§9888Ｉ…θ⁴


Output 9 if the year is a multiple of 4, otherwise 8.