6
\$\begingroup\$

Given a date, output the X and Y position of the current day of the date on a digital calendar. The digital calendar is sort of the calendar that gets shown when you click on your computers clock (at least on windows), and it looks something like this:

Digital Calendar for September 2017

If we assume that the given date is 25.09.2017, the result would be (2,5), because the 25th day of the 9th month of this year is in the second column and fifth row.

Note that if the date would be something like 29.08.2017, we couldn't use the calendar from above, but a new one for August. The gray-ish numbers are simply there to fill the gaps until the day that the month actually starts.

Input

A date, in any reasonable format.

Output

The point or coordinates that the day of the given date lies at, in its respective calendar. These should be 1-indexed.

Rules

  • This is , the shortest code in any language wins.
  • Standard loopholes are forbidden.

Additional Notes

The first column of the calendar will always be Sunday.

Here are the digital calendars from January and September 2017:

Digital Calendar for September 2017 Digital Calendar for January 2017

Test Cases

Input: 06.02.2018 (DD.MM.YYYY) or 2018.02.06 (YYYY.MM.DD)
Output: (3,2)

Input: 12.11.1982 (DD.MM.YYYY) or 1982.11.12 (YYYY.MM.DD)
Output: (6,2)

Input: 01.01.2030 (DD.MM.YYYY) or 2030.01.01 (YYYY.MM.DD)
Output: (3,1)

Input: 13.06.3017 (DD.MM.YYYY) or 3017.06.13 (YYYY.MM.DD)
Output: (6,3)

\$\endgroup\$
  • 5
    \$\begingroup\$ I recommend providing your example test-cases in the yyyy-mm-dd format to account for an international audience. \$\endgroup\$ – wizzwizz4 Sep 30 '17 at 14:46
  • 2
    \$\begingroup\$ You say 1-indexed coordinates, first column is Sunday. So Sunday is column 1, Monday is coumn 2. Yet your example says Monday is column 1. Please clarify. Voting to close as unclear until resolved \$\endgroup\$ – Luis Mendo Sep 30 '17 at 15:23
  • 2
    \$\begingroup\$ @EriktheOutgolfer This post was sandboxed. \$\endgroup\$ – Ian H. Sep 30 '17 at 16:49
  • 1
    \$\begingroup\$ @EriktheOutgolfer I always sandbox my post, simply because I'm not sure whether people will understand my challenge, although the biggest errors almost never get found in the sandbox ... \$\endgroup\$ – Ian H. Sep 30 '17 at 17:10
  • 1
    \$\begingroup\$ Is there any limit in the dates? More precisely - should we account for Gregorian vs. Julian Calendar? \$\endgroup\$ – Grzegorz Oledzki Sep 30 '17 at 18:22
3
\$\begingroup\$

Mathematica, 83 bytes

New Rules:Starting on Sunday

{s=Min@Position[(d=DayName)@{6,1,#}&/@Range@7,d@DateObject@{##}],⌈(#3-s)/7⌉+1}&   


-5 bytes from @Misha Lavrov

Input form

[2030, 1, 1]
yyyy-mm-dd

\$\endgroup\$
  • \$\begingroup\$ Nice! Do I understand correctly that {6,1,2,3,4,10,5} are there because the years 6 AD, 1 AD, ..., 10 AD, 5 AD began with Sunday through Saturday respectively? (In a hypothetical backward extension of the Gregorian calendar.) \$\endgroup\$ – Misha Lavrov Sep 30 '17 at 20:14
  • \$\begingroup\$ It was the only way to get the days of the week... Is there a better way? \$\endgroup\$ – J42161217 Sep 30 '17 at 20:30
  • \$\begingroup\$ Now that I think about it, DayName@{6,1,#}&/@Range@7 is a bit shorter than DayName@{#}&/@{6,1,2,3,4,10,5}. But it uses much the same idea, which I think is a neat one. \$\endgroup\$ – Misha Lavrov Sep 30 '17 at 20:40
3
\$\begingroup\$

JavaScript (ES6), 64 59 bytes

f=
(s,d=new Date(s),e=d.getDay())=>[1+e,(d.getDate()-e)/7+2|0]
<input oninput=o.textContent=f(this.value)><pre id=o>

Input is any string which JavaScript recognises as a date. Save 16 bytes if a JavaScript date object is acceptable input. Edit: Saved 5 bytes thanks to @GrzegorzOledzki.

\$\endgroup\$
  • \$\begingroup\$ "Input is any string which JavaScript recognises as a date." <-- This varies a lot between JavaScript implementations. \$\endgroup\$ – Ismael Miguel Sep 30 '17 at 19:22
  • \$\begingroup\$ @IsmaelMiguel Does it? I never noticed that in even if it were so it is obvious that it would only accept strings that your ecmascript implementation recognizes \$\endgroup\$ – Roman Gräf Sep 30 '17 at 20:19
  • \$\begingroup\$ You could call d.getDay() once. That is, extract the expression to a variable/argument e=d.getDay(). \$\endgroup\$ – Grzegorz Oledzki Sep 30 '17 at 21:00
2
\$\begingroup\$

TI-Basic (84+), 27 bytes (50 characters)

:Prompt Y,M,D
:dayOfWk(Y,M,D
:{Ans,2+int(7⁻¹(D-Ans
\$\endgroup\$
2
\$\begingroup\$

Python 2, 109 98 bytes

The datetime module docs are 50,000 bytes, so I'm not using the datetime module yet.

def f(d,m,y):t=m<3;y-=t;Y=y%100;W=(d+(13*m+156*t-27)/5-y/100*2+y/400+Y+Y/4)%7+1;print W,(d+13-W)/7

Try it online!

My formula to compute day of week comes from https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html.

The formula is as follows (converted from image to text):

W = (k + floor(2.6*m-0.2) - 2*C + Y + floor(Y/4) + floor(C/4)) mod 7

where floor(k) denotes the integer floor function

k is day (1 to 31)

m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat Jan & Feb as months of the preceding year C is century (1987 has C = 19)

Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb)

W is week day (0 = Sunday, ..., 6 = Saturday)

Here the century and 400 year corrections are built into the formula. The floor(2.6*m-0.2) term relates to the repetitive pattern that the 30-day months show when March is taken as the first month.

The row number can be calculated as one more than the number of Sundays in the month preceding the day.

-7 bytes thanks to @GrzegorzOledzki

\$\endgroup\$
  • \$\begingroup\$ Why do you +7? and then do modulo 7 \$\endgroup\$ – Grzegorz Oledzki Sep 30 '17 at 19:58
  • \$\begingroup\$ @GrzegorzOledzki it is possible to get a negative in the parentheses, and a negative mod 7 would give a negative in the range [-6,0]. adding 7 and taking mod 7 again casts this from 0 to 6 inclusive. \$\endgroup\$ – fireflame241 Sep 30 '17 at 20:02
  • \$\begingroup\$ -1%7 yields 6 in my python. \$\endgroup\$ – Grzegorz Oledzki Sep 30 '17 at 20:04
  • \$\begingroup\$ And couldn't (d+6-W)/7+1 be replaced with (d+13-W)/7? \$\endgroup\$ – Grzegorz Oledzki Sep 30 '17 at 20:05
  • \$\begingroup\$ @GrzegorzOledzki Good observation, and you were right with the modulo. Negative modulo output comes from a negative after %, not before. \$\endgroup\$ – fireflame241 Sep 30 '17 at 20:13

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.