Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Day of the week of an ambiguous date

Goal

Write a program or function that returns the day of the week for a date, eg.

01/06/2020 -> Mon

However, it's unknown if the date is in the format mm/dd/yyyy or dd/mm/yyyy. If you can be certain of the day of the week, return it. If there is uncertainty, return an error.

02/07/2020 -> Err (Thu? Fri?)

Input

A date in the format #/#/####.

  • The numbers can optionally have leading zeros.
  • The year should be read verbatim, assume that 19 = 19 AD, not 19 = 2019 AD.
  • The separators can be any non-digit character.
  • Assume the input date is valid in at least one format (ie. not 13/13/2020).
  • At a minimum, support dates from 2000 through 2030 (which are likely covered by any modern OS and language with a date library.)

Requirements

  • If the first and second numbers:
    • are the same (03/03/2020), the date is unambiguous.
    • either is greater than 12 (13/09/2020, 09/13/2020), the date is unambiguous.
    • are different and both 12 or less (09/11/2020), the date is ambiguous.

Output

  • If the date is unambiguous, return the day of the week.
  • If the date is ambiguous:
    • If both versions of the date have the same day of the week, return the day of the week.
    • If both versions of the date have a different day of the week, return an error.
  • A day of the week should be returned as a string of the day's three-letter abbreviation ("Mon"), case-insensitive.
  • An error should be returned as any other three-character string, not all whitespace, eg. "Err", "NaN", "???".

Test Cases

02/07/2020 -> Err
01/06/2020 -> Mon
05/05/2020 -> Tue
24/06/2020 -> Wed
05/09/2030 -> Thu
03/13/2020 -> Fri
29/02/2020 -> Sat
03/12/2000 -> Sun

Scoring

The code with the least byte count in a week wins the tick.

Answer*

Draft saved
Draft discarded
Cancel
7
  • \$\begingroup\$ "Look mah" would look less "mah" as "Looks meh" :D I would edit it in, but I'm not sure if the "mah" is the "Dutch version" of "meh" :) \$\endgroup\$ Commented Mar 19, 2020 at 9:59
  • 1
    \$\begingroup\$ You can probably get rid of \$J\$ since we're only asked to support dates from 2000 through 2030. \$\endgroup\$ Commented Mar 19, 2020 at 10:14
  • \$\begingroup\$ @Arnauld Thanks, -6 bytes by hard-coding \$+\left\lfloor {\frac {J}{4}}\right\rfloor -2J\$ to \$-35\$. :) \$\endgroup\$ Commented Mar 19, 2020 at 11:48
  • 1
    \$\begingroup\$ My bad, my previous comment was wrong, I deleted it. #Ð2£TSǝ => #ÂÀ for -4. Rotate the list of days back to what it was so you can delete the -36 (-35 is a noop mod 7). \$\endgroup\$ Commented Mar 19, 2020 at 22:55
  • 1
    \$\begingroup\$ 13*5÷ => 2*T÷ for -1. \$\endgroup\$ Commented Mar 20, 2020 at 11:02

AltStyle によって変換されたページ (->オリジナル) /