Digital Calendar

6

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)

Ian H.

Posted 2017-09-30T14:40:03.200

Reputation: 2 431

Your "September 2017" calendar seems to be a bit off... – Erik the Outgolfer – 2017-09-30T14:41:56.070

@EriktheOutgolfer My bad, fixing it. – Ian H. – 2017-09-30T14:42:59.530

5I recommend providing your example test-cases in the yyyy-mm-dd format to account for an international audience. – wizzwizz4 – 2017-09-30T14:46:27.970

Why would 2017-09-25 return (1,5) instead of (2,5) if the output is 1-indexed starting on Sunday? – LegionMammal978 – 2017-09-30T15:15:34.637

2You 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 – Luis Mendo – 2017-09-30T15:23:27.467

Could we have test cases for days distant such as year 400 or year 3017? – fireflame241 – 2017-09-30T15:25:03.250

@LuisMendo My bad, forgot to change that. Fixed it now. – Ian H. – 2017-09-30T15:40:17.720

@fireflame241 Added one for 13.06.3017. – Ian H. – 2017-09-30T15:45:22.080

I apologise for the incorrect outputs for the test cases, it seems like i had a bug in the program i wrote to calculate them. They should be fine now. – Ian H. – 2017-09-30T15:53:14.113

i think that the last case should be (6,3) - becuase the 13th in that month is friday - or js date is incorrect... – DanielIndie – 2017-09-30T16:25:17.027

@IanH. If you use the Sandbox you can fix such things before posting on main. – Erik the Outgolfer – 2017-09-30T16:40:51.097

2@EriktheOutgolfer This post was sandboxed. – Ian H. – 2017-09-30T16:49:45.260

@IanH. Yeah the sandbox isn't 100% great either. – Erik the Outgolfer – 2017-09-30T16:52:47.727

1@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 ... – Ian H. – 2017-09-30T17:10:36.950

1Is there any limit in the dates? More precisely - should we account for Gregorian vs. Julian Calendar? – Grzegorz Oledzki – 2017-09-30T18:22:16.767

A bit unclear, but I assume that any reasonable format means that we can chose the input format, as long as it is reasonable. – Titus – 2017-09-30T18:29:06.910

@GrzegorzOledzki No it's not neccessary. Maybe just specify which calendar you use in your code. – Ian H. – 2017-09-30T18:48:05.140

Answers

3

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

J42161217

Posted 2017-09-30T14:40:03.200

Reputation: 15 931

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.) – Misha Lavrov – 2017-09-30T20:14:28.767

It was the only way to get the days of the week... Is there a better way? – J42161217 – 2017-09-30T20:30:53.130

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. – Misha Lavrov – 2017-09-30T20:40:15.390

3

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.

Neil

Posted 2017-09-30T14:40:03.200

Reputation: 95 035

"Input is any string which JavaScript recognises as a date." <-- This varies a lot between JavaScript implementations. – Ismael Miguel – 2017-09-30T19:22:11.230

@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 – Roman Gräf – 2017-09-30T20:19:01.167

You could call d.getDay() once. That is, extract the expression to a variable/argument e=d.getDay(). – Grzegorz Oledzki – 2017-09-30T21:00:21.607

2

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

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

Misha Lavrov

Posted 2017-09-30T14:40:03.200

Reputation: 4 846

2

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

fireflame241

Posted 2017-09-30T14:40:03.200

Reputation: 7 021

Why do you +7? and then do modulo 7 – Grzegorz Oledzki – 2017-09-30T19:58:48.697

@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. – fireflame241 – 2017-09-30T20:02:35.057

-1%7 yields 6 in my python. – Grzegorz Oledzki – 2017-09-30T20:04:42.990

And couldn't (d+6-W)/7+1 be replaced with (d+13-W)/7? – Grzegorz Oledzki – 2017-09-30T20:05:34.090

@GrzegorzOledzki Good observation, and you were right with the modulo. Negative modulo output comes from a negative after %, not before. – fireflame241 – 2017-09-30T20:13:18.513