Today is the Center
$begingroup$
Given a date as input in any convenient format, output a calendar with that date as the exact center of a five-week window. The header of the calendar must include the two-letter abbreviations for the days of the week (i.e., Su Mo Tu We Th Fr Sa
). Three-letter or other abbreviations of the days are not allowed.
For example, given April 2 2019
as input, the output should be
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
so that the given date is the exact middle of the calendar.
Given February 19 2020
, output
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
1 2 3 4 5 6 7
For September 14 1752
, show the following:
Mo Tu We Th Fr Sa Su
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
- Input and output can be given by any convenient method.
- The input is guaranteed non-empty and valid (i.e., you'll never receive
""
orFeb 31
etc.). - Assume Gregorian calendar for all dates.
- Leap years must be accounted for.
- Input dates will range from
Jan 1 1600
toDec 31 2500
. - You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
- Leading zeros on the single-digit days are allowed, as are aligning the single-digit days to be left-aligned instead.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string ascii-art date
$endgroup$
add a comment |
$begingroup$
Given a date as input in any convenient format, output a calendar with that date as the exact center of a five-week window. The header of the calendar must include the two-letter abbreviations for the days of the week (i.e., Su Mo Tu We Th Fr Sa
). Three-letter or other abbreviations of the days are not allowed.
For example, given April 2 2019
as input, the output should be
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
so that the given date is the exact middle of the calendar.
Given February 19 2020
, output
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
1 2 3 4 5 6 7
For September 14 1752
, show the following:
Mo Tu We Th Fr Sa Su
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
- Input and output can be given by any convenient method.
- The input is guaranteed non-empty and valid (i.e., you'll never receive
""
orFeb 31
etc.). - Assume Gregorian calendar for all dates.
- Leap years must be accounted for.
- Input dates will range from
Jan 1 1600
toDec 31 2500
. - You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
- Leading zeros on the single-digit days are allowed, as are aligning the single-digit days to be left-aligned instead.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string ascii-art date
$endgroup$
add a comment |
$begingroup$
Given a date as input in any convenient format, output a calendar with that date as the exact center of a five-week window. The header of the calendar must include the two-letter abbreviations for the days of the week (i.e., Su Mo Tu We Th Fr Sa
). Three-letter or other abbreviations of the days are not allowed.
For example, given April 2 2019
as input, the output should be
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
so that the given date is the exact middle of the calendar.
Given February 19 2020
, output
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
1 2 3 4 5 6 7
For September 14 1752
, show the following:
Mo Tu We Th Fr Sa Su
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
- Input and output can be given by any convenient method.
- The input is guaranteed non-empty and valid (i.e., you'll never receive
""
orFeb 31
etc.). - Assume Gregorian calendar for all dates.
- Leap years must be accounted for.
- Input dates will range from
Jan 1 1600
toDec 31 2500
. - You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
- Leading zeros on the single-digit days are allowed, as are aligning the single-digit days to be left-aligned instead.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string ascii-art date
$endgroup$
Given a date as input in any convenient format, output a calendar with that date as the exact center of a five-week window. The header of the calendar must include the two-letter abbreviations for the days of the week (i.e., Su Mo Tu We Th Fr Sa
). Three-letter or other abbreviations of the days are not allowed.
For example, given April 2 2019
as input, the output should be
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
so that the given date is the exact middle of the calendar.
Given February 19 2020
, output
Su Mo Tu We Th Fr Sa
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
1 2 3 4 5 6 7
For September 14 1752
, show the following:
Mo Tu We Th Fr Sa Su
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 1
- Input and output can be given by any convenient method.
- The input is guaranteed non-empty and valid (i.e., you'll never receive
""
orFeb 31
etc.). - Assume Gregorian calendar for all dates.
- Leap years must be accounted for.
- Input dates will range from
Jan 1 1600
toDec 31 2500
. - You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
- Leading zeros on the single-digit days are allowed, as are aligning the single-digit days to be left-aligned instead.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf string ascii-art date
code-golf string ascii-art date
edited Apr 5 at 12:25
AdmBorkBork
asked Apr 4 at 19:13
AdmBorkBorkAdmBorkBork
27.8k467239
27.8k467239
add a comment |
add a comment |
14 Answers
14
active
oldest
votes
$begingroup$
R, 77 72 bytes
function(d,`~`=format)write(c(strtrim(d+-3:3~"%a",2),d+-17:17~"%e"),1,7)
Try it online!
Fixed output to use 2 letter day abbreviations.
-1 byte using strtrim
thanks to Aaron Hayman.
Pads date numbers with leading 0s; takes input as a Date
, which can be created by using as.Date("YYYY/MM/DD")
.
Weirdly short for an R ascii-art answer...
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 141 126 bytes
Saved 15 bytes by borrowing .toUTCString().slice(0,2)
from Neil's answer
Takes input as a Date object.
f=(d,n=0)=>n<42?(D=new Date(d-864e5*(24-n)),n<7?D.toUTCString().slice(0,2):(i=D.getDate())>9?i:' '+i)+`
`[++n%7&&1]+f(d,n):''
Try it online!
$endgroup$
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
add a comment |
$begingroup$
05AB1E, 175 174 172 171 160 bytes
¦WΘ1š-1šVтFY`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝVY})DJIJk18+£35.£¬.•4ιõ÷‡o‹ƶ¸•2ôs`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%._s€нT‰J«7ô»
Input in the format [day, month, year]
. Output with leading 0
s for single-digit days, and lowercase mo
through su
(+1 byte can be added if titlecase is mandatory).
Try it online or verify all test cases.
Holy shit.. This might be my new record for longest 05AB1E answer, and then I include some very complex ascii-art challenges I did... >.> EDIT: Hmm ok, almost.. ;p
Important note: 05AB1E doesn't have any builtins for Date objects or calculations. The only builtin regarding dates it has is today's year/month/day/hours/minutes/seconds/microseconds.
So because of that, almost all of the code you see are manual calculations to calculated the previous and next days (including transition over years and keeping in mind the leap years), and calculating the day of the week by using Zeller's congruence.
Huge parts of the code are copied from this earlier 05AB1E answer of mine, which will also be relevant for the explanation below.
Explanation:
We start by going to the first day of the previous month:
¦ # Remove the first item (the days) from the (implicit) input
W # Get the minimum (without popping the list itself)
# (since the year is guaranteed to be above 1599, this is the month)
Θ # Check if its exactly 1 (1 if 1, 0 if in the range [2,31])
1š # Prepend a 1 as list (so we now have either [1,1] or [1,0]
- # Subtract this from the month and year
1š # And prepend a 1 for the day
V # Pop and store this first day of the previous month in variable `Y`
Then I use that date as start date, and calculate the next 100 days:
тF # Loop 100 times:
Y`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝV
# Calculate the next day in line
# (see the linked challenge above for a detailed explanation of this)
Y # And leave it on the stack
}) # After the loop: wrap the entire stack into a list, which contains our 100 days
Then, with the input-date as the middle, I only leave the 17 before and 17 after that input-date from the list:
DJ # Duplicate the 100 dates, and join the day/month/year together to strings
IJ # Push the input, also joined together
k # Get the 0-based index of the input in this list
# (the joins are necessary, because indexing doesn't work for 2D lists)
18+ # Add 18 to this index (18 instead of 17, because the index is 0-based)
£ # Only leave the first index+18 items from the 100 dates
35.£ # Then only leave the last 35 items
Now we have our 35 days. Next step is to calculate the day of the week, and create the header of the output-table:
¬ # Get the first date of the list (without popping the list itself)
.•4ιõ÷‡o‹ƶ¸• # Push compressed string "sasumotuwethfr"
2ô # Split it into chunks of size 2
s # Swap to get the first date again
`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%
# Calculate the day of the week (sa=0; su=1; ...; fr=6)
# (see the linked challenge above for a detailed explanation of this)
._ # Rotate the list of strings that many times
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•4ιõ÷‡o‹ƶ¸•
is "sasumotuwethfr"
.
Then we create the days to fill the table itself based on our earlier created list of dates. Which we'll merge together with the header. After which we can print the final result:
s # Swap to get the list of dates again
€н # Only leave the first item of each date (the days)
T‰ # Take the divmod 10 of each
J # Join those divmod results together
# (we now have leading 0s for single-digit days)
« # Merge this list together with the header list
7ô # Split it into chunks of size 7
» # Join each inner list by spaces, and then each string by newlines
# (and output the result implicitly)
$endgroup$
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
|
show 2 more comments
$begingroup$
JavaScript (Node.js), 205 152 145 bytes
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
Try it online! Takes input as JavaScript Date object or timestamp. Edit: Saved 1 byte thanks to @EmbodimentofIgnorance, which then allowed me to save a further 7 bytes by adding a trailing newline to the output. Saved 52 bytes when I discovered that I was working around behaviour that was not actually buggy in the first place...
$endgroup$
$begingroup$
padStart(2)
->padStart(3)
, remove the space in the join string for -1 byte
$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
add a comment |
$begingroup$
Perl 6, 87 bytes
{~rotate(<Th Fr Sa Su Mo Tu We>,.day-of-week),|comb 21,($_-17..$_+17)>>.day.fmt('%2d')}
Try it online!
Takes a Date
object, returns a list of lines.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 124 120 bytes
n=>{for(int i=0;i<42;)Write($"{(i<7?$"{n.AddDays(i-3):ddd}".Remove(2,1):n.AddDays(i-24).Day+""),3}"+(++i%7<1?"n":""));}
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 123 bytes
(s=#;Grid@Join[{StringTake[ToString@DayName[s~d~#]&/@Range[-3,3],2]},Partition[Last@d[s,#]&/@Range[-17,17],7]])&
d=DatePlus
Try it online!
I don't know why Grid
doesn't work on TIO but this code outputs this
@DavidC saved 1 byte
$endgroup$
$begingroup$
MaybeGrid
doesn't work because TIO can't center the items like in your picture?
$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
add a comment |
$begingroup$
MATL, 34 33 31 bytes
YO-17:17+t7:)8XOO3Z(!1ew7XOU7e!
Try it online!
Explanation
YO % Implicit input. Convert to date number. This is a single number
% that specifies the date
-17:17 % Push [-17 -16 ... 16 17]
+ % Add to date number, element-wise. This gives a row vector of 35
% date numbers centered around the input date
t % Duplicate
7: % Push [1 2 ... 7]
) % Index into the 35-element vector. This keeps the first 7 entries
8XO % Convert to day-of-week in 3 letters. Gives a 3-col char matrix
O3Z( % Write char 0 (equivalent to space for display purposes) into the
% 3rd column
!1e % Tranpose and linearize into a row. This produces a string such as
% 'Tu We Th Fr Sa Su Mo ', to be used as column headings
w % Swap. This brings to top the row vector of 35 date numbers
% computed from the input
7XO % Convert to day-of-month. Gives a 2-col char matrix
U % Convert each row to number
7e! % Reshape into 7-row matrix and transpose
% Implicit display. This prints the string with the headings and
% the matrix. The latter has a minimum-one-space separation between
% columns, so it is aligned with the headings
$endgroup$
add a comment |
$begingroup$
Java (JDK), 149 bytes
d->{d.add(5,-24);for(int i=0,w;i<42;d.add(5,1))System.out.printf("%c%2s",i%7<1?10:32,i++<7?"SaSuMoTuWeThFr".substring(w=d.get(7)%7*2,w+2):d.get(5));}
Try it online!
Credits
- -13 bytes thanks to Kevin Cruijssen.
$endgroup$
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
$begingroup$
Maybe you see something more to combine thei
andj
somehow? Or something shorter forj++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does thew
stand for? Why noth
for header?
$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
$begingroup$
@KevinCruijssenw
for "day of week". Also, bit twiddling can only lead to(i%7+6)/7*22+10
which is much longer.
$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
|
show 5 more comments
$begingroup$
PHP, 197 189 187 bytes
for($d=date_create($argn)->sub($i=new DateInterval(P17D)),$i->d=1;$x++<35;$h.=$x<8?substr($d->format(D),0,2).' ':'',$d->add($i))$o.=str_pad($d->format(j),3,' ',2);echo wordwrap($h.$o,20);
Try it online!
Input is STDIN
as a date string. Run with php -nF
.
$ echo April 2 2019|php -nF cal.php
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
Verify all test cases
Or 174 bytes with zero-padded single digits.
$endgroup$
add a comment |
$begingroup$
Red, 153 131 bytes
func[d][s: d - 24 loop 7[prin[""copy/part system/locale/days/(s/10) 2]s:
s + 1]loop 5[print""loop 7[prin pad/left s/4 3 s: s + 1]]]
Try it online!
$endgroup$
add a comment |
$begingroup$
T-SQL, 203 bytes
DECLARE @f date='2020-02-19'
,@ char(20)=0,@d char(105)=0SELECT
@=left(format(d,'D'),2)+' '+@,@d=right(d,2)+char(32-n%7/6*19)+@d
FROM(SELECT dateadd(d,number-17,@f)d,number n
FROM spt_values WHERE'P'=type)x ORDER BY-n
PRINT @+'
'+@d
The online version is slightly different, this posted version works in MS-SQL Studio Management. It saves 1 bytes compared with the online version, but doesn't give the correct result online
Try it online
$endgroup$
add a comment |
$begingroup$
Python 2, 115 bytes
from datetime import*
d=input()
for i in range(42):print(d+timedelta(i-24)).strftime('%'+'da'[i<7])[:2]+i%7/6*'n',
Try it online!
Not sure if this is allowed... takes input from STDIN in the form date(year, month, day)
. This can also be represented as __import__('datetime').date(year, month, day)
. These are really __import__('datetime').date
objects.
$endgroup$
add a comment |
$begingroup$
Excel VBA, 190 bytes
Function z(i)
Dim d(5, 6)
v = DateValue(i) - 17
For x = 1 To 5
For y = 0 To 6
d(0, y) = Left(WeekdayName(Weekday(v + y)), 2)
d(x, y) = day(v + y + (x - 1) * 7)
Next
Next
z = d()
End Function
Takes input in the form of a valid date string for Excel VBA (e.g. February 19, 2020; 2/19/2020; 19-Feb-2019), and returns an array with the given calendar centered on it.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182681%2ftoday-is-the-center%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
14 Answers
14
active
oldest
votes
14 Answers
14
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
R, 77 72 bytes
function(d,`~`=format)write(c(strtrim(d+-3:3~"%a",2),d+-17:17~"%e"),1,7)
Try it online!
Fixed output to use 2 letter day abbreviations.
-1 byte using strtrim
thanks to Aaron Hayman.
Pads date numbers with leading 0s; takes input as a Date
, which can be created by using as.Date("YYYY/MM/DD")
.
Weirdly short for an R ascii-art answer...
$endgroup$
add a comment |
$begingroup$
R, 77 72 bytes
function(d,`~`=format)write(c(strtrim(d+-3:3~"%a",2),d+-17:17~"%e"),1,7)
Try it online!
Fixed output to use 2 letter day abbreviations.
-1 byte using strtrim
thanks to Aaron Hayman.
Pads date numbers with leading 0s; takes input as a Date
, which can be created by using as.Date("YYYY/MM/DD")
.
Weirdly short for an R ascii-art answer...
$endgroup$
add a comment |
$begingroup$
R, 77 72 bytes
function(d,`~`=format)write(c(strtrim(d+-3:3~"%a",2),d+-17:17~"%e"),1,7)
Try it online!
Fixed output to use 2 letter day abbreviations.
-1 byte using strtrim
thanks to Aaron Hayman.
Pads date numbers with leading 0s; takes input as a Date
, which can be created by using as.Date("YYYY/MM/DD")
.
Weirdly short for an R ascii-art answer...
$endgroup$
R, 77 72 bytes
function(d,`~`=format)write(c(strtrim(d+-3:3~"%a",2),d+-17:17~"%e"),1,7)
Try it online!
Fixed output to use 2 letter day abbreviations.
-1 byte using strtrim
thanks to Aaron Hayman.
Pads date numbers with leading 0s; takes input as a Date
, which can be created by using as.Date("YYYY/MM/DD")
.
Weirdly short for an R ascii-art answer...
edited Apr 5 at 14:58
answered Apr 4 at 22:38
GiuseppeGiuseppe
17.7k31153
17.7k31153
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 141 126 bytes
Saved 15 bytes by borrowing .toUTCString().slice(0,2)
from Neil's answer
Takes input as a Date object.
f=(d,n=0)=>n<42?(D=new Date(d-864e5*(24-n)),n<7?D.toUTCString().slice(0,2):(i=D.getDate())>9?i:' '+i)+`
`[++n%7&&1]+f(d,n):''
Try it online!
$endgroup$
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
add a comment |
$begingroup$
JavaScript (ES6), 141 126 bytes
Saved 15 bytes by borrowing .toUTCString().slice(0,2)
from Neil's answer
Takes input as a Date object.
f=(d,n=0)=>n<42?(D=new Date(d-864e5*(24-n)),n<7?D.toUTCString().slice(0,2):(i=D.getDate())>9?i:' '+i)+`
`[++n%7&&1]+f(d,n):''
Try it online!
$endgroup$
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
add a comment |
$begingroup$
JavaScript (ES6), 141 126 bytes
Saved 15 bytes by borrowing .toUTCString().slice(0,2)
from Neil's answer
Takes input as a Date object.
f=(d,n=0)=>n<42?(D=new Date(d-864e5*(24-n)),n<7?D.toUTCString().slice(0,2):(i=D.getDate())>9?i:' '+i)+`
`[++n%7&&1]+f(d,n):''
Try it online!
$endgroup$
JavaScript (ES6), 141 126 bytes
Saved 15 bytes by borrowing .toUTCString().slice(0,2)
from Neil's answer
Takes input as a Date object.
f=(d,n=0)=>n<42?(D=new Date(d-864e5*(24-n)),n<7?D.toUTCString().slice(0,2):(i=D.getDate())>9?i:' '+i)+`
`[++n%7&&1]+f(d,n):''
Try it online!
edited Apr 4 at 23:44
answered Apr 4 at 23:35
ArnauldArnauld
80.8k797334
80.8k797334
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
add a comment |
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
$begingroup$
Huh, I could have sworn that my code failed for the third test case when I originally wrote it... well that saves me 52 bytes...
$endgroup$
– Neil
Apr 6 at 12:01
add a comment |
$begingroup$
05AB1E, 175 174 172 171 160 bytes
¦WΘ1š-1šVтFY`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝVY})DJIJk18+£35.£¬.•4ιõ÷‡o‹ƶ¸•2ôs`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%._s€нT‰J«7ô»
Input in the format [day, month, year]
. Output with leading 0
s for single-digit days, and lowercase mo
through su
(+1 byte can be added if titlecase is mandatory).
Try it online or verify all test cases.
Holy shit.. This might be my new record for longest 05AB1E answer, and then I include some very complex ascii-art challenges I did... >.> EDIT: Hmm ok, almost.. ;p
Important note: 05AB1E doesn't have any builtins for Date objects or calculations. The only builtin regarding dates it has is today's year/month/day/hours/minutes/seconds/microseconds.
So because of that, almost all of the code you see are manual calculations to calculated the previous and next days (including transition over years and keeping in mind the leap years), and calculating the day of the week by using Zeller's congruence.
Huge parts of the code are copied from this earlier 05AB1E answer of mine, which will also be relevant for the explanation below.
Explanation:
We start by going to the first day of the previous month:
¦ # Remove the first item (the days) from the (implicit) input
W # Get the minimum (without popping the list itself)
# (since the year is guaranteed to be above 1599, this is the month)
Θ # Check if its exactly 1 (1 if 1, 0 if in the range [2,31])
1š # Prepend a 1 as list (so we now have either [1,1] or [1,0]
- # Subtract this from the month and year
1š # And prepend a 1 for the day
V # Pop and store this first day of the previous month in variable `Y`
Then I use that date as start date, and calculate the next 100 days:
тF # Loop 100 times:
Y`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝV
# Calculate the next day in line
# (see the linked challenge above for a detailed explanation of this)
Y # And leave it on the stack
}) # After the loop: wrap the entire stack into a list, which contains our 100 days
Then, with the input-date as the middle, I only leave the 17 before and 17 after that input-date from the list:
DJ # Duplicate the 100 dates, and join the day/month/year together to strings
IJ # Push the input, also joined together
k # Get the 0-based index of the input in this list
# (the joins are necessary, because indexing doesn't work for 2D lists)
18+ # Add 18 to this index (18 instead of 17, because the index is 0-based)
£ # Only leave the first index+18 items from the 100 dates
35.£ # Then only leave the last 35 items
Now we have our 35 days. Next step is to calculate the day of the week, and create the header of the output-table:
¬ # Get the first date of the list (without popping the list itself)
.•4ιõ÷‡o‹ƶ¸• # Push compressed string "sasumotuwethfr"
2ô # Split it into chunks of size 2
s # Swap to get the first date again
`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%
# Calculate the day of the week (sa=0; su=1; ...; fr=6)
# (see the linked challenge above for a detailed explanation of this)
._ # Rotate the list of strings that many times
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•4ιõ÷‡o‹ƶ¸•
is "sasumotuwethfr"
.
Then we create the days to fill the table itself based on our earlier created list of dates. Which we'll merge together with the header. After which we can print the final result:
s # Swap to get the list of dates again
€н # Only leave the first item of each date (the days)
T‰ # Take the divmod 10 of each
J # Join those divmod results together
# (we now have leading 0s for single-digit days)
« # Merge this list together with the header list
7ô # Split it into chunks of size 7
» # Join each inner list by spaces, and then each string by newlines
# (and output the result implicitly)
$endgroup$
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
|
show 2 more comments
$begingroup$
05AB1E, 175 174 172 171 160 bytes
¦WΘ1š-1šVтFY`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝVY})DJIJk18+£35.£¬.•4ιõ÷‡o‹ƶ¸•2ôs`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%._s€нT‰J«7ô»
Input in the format [day, month, year]
. Output with leading 0
s for single-digit days, and lowercase mo
through su
(+1 byte can be added if titlecase is mandatory).
Try it online or verify all test cases.
Holy shit.. This might be my new record for longest 05AB1E answer, and then I include some very complex ascii-art challenges I did... >.> EDIT: Hmm ok, almost.. ;p
Important note: 05AB1E doesn't have any builtins for Date objects or calculations. The only builtin regarding dates it has is today's year/month/day/hours/minutes/seconds/microseconds.
So because of that, almost all of the code you see are manual calculations to calculated the previous and next days (including transition over years and keeping in mind the leap years), and calculating the day of the week by using Zeller's congruence.
Huge parts of the code are copied from this earlier 05AB1E answer of mine, which will also be relevant for the explanation below.
Explanation:
We start by going to the first day of the previous month:
¦ # Remove the first item (the days) from the (implicit) input
W # Get the minimum (without popping the list itself)
# (since the year is guaranteed to be above 1599, this is the month)
Θ # Check if its exactly 1 (1 if 1, 0 if in the range [2,31])
1š # Prepend a 1 as list (so we now have either [1,1] or [1,0]
- # Subtract this from the month and year
1š # And prepend a 1 for the day
V # Pop and store this first day of the previous month in variable `Y`
Then I use that date as start date, and calculate the next 100 days:
тF # Loop 100 times:
Y`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝV
# Calculate the next day in line
# (see the linked challenge above for a detailed explanation of this)
Y # And leave it on the stack
}) # After the loop: wrap the entire stack into a list, which contains our 100 days
Then, with the input-date as the middle, I only leave the 17 before and 17 after that input-date from the list:
DJ # Duplicate the 100 dates, and join the day/month/year together to strings
IJ # Push the input, also joined together
k # Get the 0-based index of the input in this list
# (the joins are necessary, because indexing doesn't work for 2D lists)
18+ # Add 18 to this index (18 instead of 17, because the index is 0-based)
£ # Only leave the first index+18 items from the 100 dates
35.£ # Then only leave the last 35 items
Now we have our 35 days. Next step is to calculate the day of the week, and create the header of the output-table:
¬ # Get the first date of the list (without popping the list itself)
.•4ιõ÷‡o‹ƶ¸• # Push compressed string "sasumotuwethfr"
2ô # Split it into chunks of size 2
s # Swap to get the first date again
`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%
# Calculate the day of the week (sa=0; su=1; ...; fr=6)
# (see the linked challenge above for a detailed explanation of this)
._ # Rotate the list of strings that many times
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•4ιõ÷‡o‹ƶ¸•
is "sasumotuwethfr"
.
Then we create the days to fill the table itself based on our earlier created list of dates. Which we'll merge together with the header. After which we can print the final result:
s # Swap to get the list of dates again
€н # Only leave the first item of each date (the days)
T‰ # Take the divmod 10 of each
J # Join those divmod results together
# (we now have leading 0s for single-digit days)
« # Merge this list together with the header list
7ô # Split it into chunks of size 7
» # Join each inner list by spaces, and then each string by newlines
# (and output the result implicitly)
$endgroup$
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
|
show 2 more comments
$begingroup$
05AB1E, 175 174 172 171 160 bytes
¦WΘ1š-1šVтFY`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝVY})DJIJk18+£35.£¬.•4ιõ÷‡o‹ƶ¸•2ôs`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%._s€нT‰J«7ô»
Input in the format [day, month, year]
. Output with leading 0
s for single-digit days, and lowercase mo
through su
(+1 byte can be added if titlecase is mandatory).
Try it online or verify all test cases.
Holy shit.. This might be my new record for longest 05AB1E answer, and then I include some very complex ascii-art challenges I did... >.> EDIT: Hmm ok, almost.. ;p
Important note: 05AB1E doesn't have any builtins for Date objects or calculations. The only builtin regarding dates it has is today's year/month/day/hours/minutes/seconds/microseconds.
So because of that, almost all of the code you see are manual calculations to calculated the previous and next days (including transition over years and keeping in mind the leap years), and calculating the day of the week by using Zeller's congruence.
Huge parts of the code are copied from this earlier 05AB1E answer of mine, which will also be relevant for the explanation below.
Explanation:
We start by going to the first day of the previous month:
¦ # Remove the first item (the days) from the (implicit) input
W # Get the minimum (without popping the list itself)
# (since the year is guaranteed to be above 1599, this is the month)
Θ # Check if its exactly 1 (1 if 1, 0 if in the range [2,31])
1š # Prepend a 1 as list (so we now have either [1,1] or [1,0]
- # Subtract this from the month and year
1š # And prepend a 1 for the day
V # Pop and store this first day of the previous month in variable `Y`
Then I use that date as start date, and calculate the next 100 days:
тF # Loop 100 times:
Y`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝV
# Calculate the next day in line
# (see the linked challenge above for a detailed explanation of this)
Y # And leave it on the stack
}) # After the loop: wrap the entire stack into a list, which contains our 100 days
Then, with the input-date as the middle, I only leave the 17 before and 17 after that input-date from the list:
DJ # Duplicate the 100 dates, and join the day/month/year together to strings
IJ # Push the input, also joined together
k # Get the 0-based index of the input in this list
# (the joins are necessary, because indexing doesn't work for 2D lists)
18+ # Add 18 to this index (18 instead of 17, because the index is 0-based)
£ # Only leave the first index+18 items from the 100 dates
35.£ # Then only leave the last 35 items
Now we have our 35 days. Next step is to calculate the day of the week, and create the header of the output-table:
¬ # Get the first date of the list (without popping the list itself)
.•4ιõ÷‡o‹ƶ¸• # Push compressed string "sasumotuwethfr"
2ô # Split it into chunks of size 2
s # Swap to get the first date again
`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%
# Calculate the day of the week (sa=0; su=1; ...; fr=6)
# (see the linked challenge above for a detailed explanation of this)
._ # Rotate the list of strings that many times
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•4ιõ÷‡o‹ƶ¸•
is "sasumotuwethfr"
.
Then we create the days to fill the table itself based on our earlier created list of dates. Which we'll merge together with the header. After which we can print the final result:
s # Swap to get the list of dates again
€н # Only leave the first item of each date (the days)
T‰ # Take the divmod 10 of each
J # Join those divmod results together
# (we now have leading 0s for single-digit days)
« # Merge this list together with the header list
7ô # Split it into chunks of size 7
» # Join each inner list by spaces, and then each string by newlines
# (and output the result implicitly)
$endgroup$
05AB1E, 175 174 172 171 160 bytes
¦WΘ1š-1šVтFY`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝVY})DJIJk18+£35.£¬.•4ιõ÷‡o‹ƶ¸•2ôs`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%._s€нT‰J«7ô»
Input in the format [day, month, year]
. Output with leading 0
s for single-digit days, and lowercase mo
through su
(+1 byte can be added if titlecase is mandatory).
Try it online or verify all test cases.
Holy shit.. This might be my new record for longest 05AB1E answer, and then I include some very complex ascii-art challenges I did... >.> EDIT: Hmm ok, almost.. ;p
Important note: 05AB1E doesn't have any builtins for Date objects or calculations. The only builtin regarding dates it has is today's year/month/day/hours/minutes/seconds/microseconds.
So because of that, almost all of the code you see are manual calculations to calculated the previous and next days (including transition over years and keeping in mind the leap years), and calculating the day of the week by using Zeller's congruence.
Huge parts of the code are copied from this earlier 05AB1E answer of mine, which will also be relevant for the explanation below.
Explanation:
We start by going to the first day of the previous month:
¦ # Remove the first item (the days) from the (implicit) input
W # Get the minimum (without popping the list itself)
# (since the year is guaranteed to be above 1599, this is the month)
Θ # Check if its exactly 1 (1 if 1, 0 if in the range [2,31])
1š # Prepend a 1 as list (so we now have either [1,1] or [1,0]
- # Subtract this from the month and year
1š # And prepend a 1 for the day
V # Pop and store this first day of the previous month in variable `Y`
Then I use that date as start date, and calculate the next 100 days:
тF # Loop 100 times:
Y`2ô0Kθ4ÖUD2Qi28X+ë<7%É31α}‹iY¬>0ëY1¾ǝDÅsD12‹i>1ë1Dǝ¤>2}}ǝV
# Calculate the next day in line
# (see the linked challenge above for a detailed explanation of this)
Y # And leave it on the stack
}) # After the loop: wrap the entire stack into a list, which contains our 100 days
Then, with the input-date as the middle, I only leave the 17 before and 17 after that input-date from the list:
DJ # Duplicate the 100 dates, and join the day/month/year together to strings
IJ # Push the input, also joined together
k # Get the 0-based index of the input in this list
# (the joins are necessary, because indexing doesn't work for 2D lists)
18+ # Add 18 to this index (18 instead of 17, because the index is 0-based)
£ # Only leave the first index+18 items from the 100 dates
35.£ # Then only leave the last 35 items
Now we have our 35 days. Next step is to calculate the day of the week, and create the header of the output-table:
¬ # Get the first date of the list (without popping the list itself)
.•4ιõ÷‡o‹ƶ¸• # Push compressed string "sasumotuwethfr"
2ô # Split it into chunks of size 2
s # Swap to get the first date again
`UÐ3‹12*+>13*5÷s3‹Xα©т%D4÷®т÷©4÷®·()DćsćsO7%
# Calculate the day of the week (sa=0; su=1; ...; fr=6)
# (see the linked challenge above for a detailed explanation of this)
._ # Rotate the list of strings that many times
See this 05AB1E tip of mine (section How to compress strings not part of the dictionary?) to understand why .•4ιõ÷‡o‹ƶ¸•
is "sasumotuwethfr"
.
Then we create the days to fill the table itself based on our earlier created list of dates. Which we'll merge together with the header. After which we can print the final result:
s # Swap to get the list of dates again
€н # Only leave the first item of each date (the days)
T‰ # Take the divmod 10 of each
J # Join those divmod results together
# (we now have leading 0s for single-digit days)
« # Merge this list together with the header list
7ô # Split it into chunks of size 7
» # Join each inner list by spaces, and then each string by newlines
# (and output the result implicitly)
edited Apr 5 at 13:50
answered Apr 5 at 10:27
Kevin CruijssenKevin Cruijssen
42.7k571217
42.7k571217
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
|
show 2 more comments
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
2
2
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
$begingroup$
That's a huge amount of work!
$endgroup$
– Luis Mendo
Apr 5 at 10:41
2
2
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
Yeah, Java beats 05AB1E! :D First time ever, I guess ;-)
$endgroup$
– Olivier Grégoire
Apr 5 at 10:44
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@LuisMendo Most was done last time with the linked challenge, but yes, it was a lot of work.. ;) Explanation has been added btw.
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:00
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire Now we're the same bye-count. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 11:17
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
$begingroup$
@OlivierGrégoire And now it's lower again, sorry. ;p
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:09
|
show 2 more comments
$begingroup$
JavaScript (Node.js), 205 152 145 bytes
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
Try it online! Takes input as JavaScript Date object or timestamp. Edit: Saved 1 byte thanks to @EmbodimentofIgnorance, which then allowed me to save a further 7 bytes by adding a trailing newline to the output. Saved 52 bytes when I discovered that I was working around behaviour that was not actually buggy in the first place...
$endgroup$
$begingroup$
padStart(2)
->padStart(3)
, remove the space in the join string for -1 byte
$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
add a comment |
$begingroup$
JavaScript (Node.js), 205 152 145 bytes
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
Try it online! Takes input as JavaScript Date object or timestamp. Edit: Saved 1 byte thanks to @EmbodimentofIgnorance, which then allowed me to save a further 7 bytes by adding a trailing newline to the output. Saved 52 bytes when I discovered that I was working around behaviour that was not actually buggy in the first place...
$endgroup$
$begingroup$
padStart(2)
->padStart(3)
, remove the space in the join string for -1 byte
$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
add a comment |
$begingroup$
JavaScript (Node.js), 205 152 145 bytes
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
Try it online! Takes input as JavaScript Date object or timestamp. Edit: Saved 1 byte thanks to @EmbodimentofIgnorance, which then allowed me to save a further 7 bytes by adding a trailing newline to the output. Saved 52 bytes when I discovered that I was working around behaviour that was not actually buggy in the first place...
$endgroup$
JavaScript (Node.js), 205 152 145 bytes
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
Try it online! Takes input as JavaScript Date object or timestamp. Edit: Saved 1 byte thanks to @EmbodimentofIgnorance, which then allowed me to save a further 7 bytes by adding a trailing newline to the output. Saved 52 bytes when I discovered that I was working around behaviour that was not actually buggy in the first place...
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
f=
d=>`012345`.replace(g=/./g,r=>`0123456
`.replace(g,c=>`${new Date(d-864e5*(24-c-r*7))[+r?`getUTCDate`:`toUTCString`]()}`.slice(0,2).padStart(3)))
<input type=date oninput=o.textContent=f(this.valueAsDate)><pre id=o>
edited Apr 6 at 20:07
answered Apr 4 at 22:26
NeilNeil
82.8k745179
82.8k745179
$begingroup$
padStart(2)
->padStart(3)
, remove the space in the join string for -1 byte
$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
add a comment |
$begingroup$
padStart(2)
->padStart(3)
, remove the space in the join string for -1 byte
$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
$begingroup$
padStart(2)
-> padStart(3)
, remove the space in the join string for -1 byte$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
$begingroup$
padStart(2)
-> padStart(3)
, remove the space in the join string for -1 byte$endgroup$
– Embodiment of Ignorance
Apr 4 at 22:37
add a comment |
$begingroup$
Perl 6, 87 bytes
{~rotate(<Th Fr Sa Su Mo Tu We>,.day-of-week),|comb 21,($_-17..$_+17)>>.day.fmt('%2d')}
Try it online!
Takes a Date
object, returns a list of lines.
$endgroup$
add a comment |
$begingroup$
Perl 6, 87 bytes
{~rotate(<Th Fr Sa Su Mo Tu We>,.day-of-week),|comb 21,($_-17..$_+17)>>.day.fmt('%2d')}
Try it online!
Takes a Date
object, returns a list of lines.
$endgroup$
add a comment |
$begingroup$
Perl 6, 87 bytes
{~rotate(<Th Fr Sa Su Mo Tu We>,.day-of-week),|comb 21,($_-17..$_+17)>>.day.fmt('%2d')}
Try it online!
Takes a Date
object, returns a list of lines.
$endgroup$
Perl 6, 87 bytes
{~rotate(<Th Fr Sa Su Mo Tu We>,.day-of-week),|comb 21,($_-17..$_+17)>>.day.fmt('%2d')}
Try it online!
Takes a Date
object, returns a list of lines.
edited Apr 5 at 9:28
answered Apr 5 at 9:19
nwellnhofnwellnhof
7,46011128
7,46011128
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 124 120 bytes
n=>{for(int i=0;i<42;)Write($"{(i<7?$"{n.AddDays(i-3):ddd}".Remove(2,1):n.AddDays(i-24).Day+""),3}"+(++i%7<1?"n":""));}
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 124 120 bytes
n=>{for(int i=0;i<42;)Write($"{(i<7?$"{n.AddDays(i-3):ddd}".Remove(2,1):n.AddDays(i-24).Day+""),3}"+(++i%7<1?"n":""));}
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 124 120 bytes
n=>{for(int i=0;i<42;)Write($"{(i<7?$"{n.AddDays(i-3):ddd}".Remove(2,1):n.AddDays(i-24).Day+""),3}"+(++i%7<1?"n":""));}
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 124 120 bytes
n=>{for(int i=0;i<42;)Write($"{(i<7?$"{n.AddDays(i-3):ddd}".Remove(2,1):n.AddDays(i-24).Day+""),3}"+(++i%7<1?"n":""));}
Try it online!
edited Apr 5 at 16:22
answered Apr 4 at 20:56
Embodiment of IgnoranceEmbodiment of Ignorance
2,946127
2,946127
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 123 bytes
(s=#;Grid@Join[{StringTake[ToString@DayName[s~d~#]&/@Range[-3,3],2]},Partition[Last@d[s,#]&/@Range[-17,17],7]])&
d=DatePlus
Try it online!
I don't know why Grid
doesn't work on TIO but this code outputs this
@DavidC saved 1 byte
$endgroup$
$begingroup$
MaybeGrid
doesn't work because TIO can't center the items like in your picture?
$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
add a comment |
$begingroup$
Wolfram Language (Mathematica), 123 bytes
(s=#;Grid@Join[{StringTake[ToString@DayName[s~d~#]&/@Range[-3,3],2]},Partition[Last@d[s,#]&/@Range[-17,17],7]])&
d=DatePlus
Try it online!
I don't know why Grid
doesn't work on TIO but this code outputs this
@DavidC saved 1 byte
$endgroup$
$begingroup$
MaybeGrid
doesn't work because TIO can't center the items like in your picture?
$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
add a comment |
$begingroup$
Wolfram Language (Mathematica), 123 bytes
(s=#;Grid@Join[{StringTake[ToString@DayName[s~d~#]&/@Range[-3,3],2]},Partition[Last@d[s,#]&/@Range[-17,17],7]])&
d=DatePlus
Try it online!
I don't know why Grid
doesn't work on TIO but this code outputs this
@DavidC saved 1 byte
$endgroup$
Wolfram Language (Mathematica), 123 bytes
(s=#;Grid@Join[{StringTake[ToString@DayName[s~d~#]&/@Range[-3,3],2]},Partition[Last@d[s,#]&/@Range[-17,17],7]])&
d=DatePlus
Try it online!
I don't know why Grid
doesn't work on TIO but this code outputs this
@DavidC saved 1 byte
edited Apr 5 at 8:28
answered Apr 4 at 20:25
J42161217J42161217
14k21353
14k21353
$begingroup$
MaybeGrid
doesn't work because TIO can't center the items like in your picture?
$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
add a comment |
$begingroup$
MaybeGrid
doesn't work because TIO can't center the items like in your picture?
$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
$begingroup$
Maybe
Grid
doesn't work because TIO can't center the items like in your picture?$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
Maybe
Grid
doesn't work because TIO can't center the items like in your picture?$endgroup$
– AdmBorkBork
Apr 5 at 12:27
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
$begingroup$
@AdmBorkBork There is a way to load graphics like this in TIO. Someone had shown me last year I think. But i can't remember how to do it... So if anyone knows, let us know!
$endgroup$
– J42161217
Apr 5 at 12:36
add a comment |
$begingroup$
MATL, 34 33 31 bytes
YO-17:17+t7:)8XOO3Z(!1ew7XOU7e!
Try it online!
Explanation
YO % Implicit input. Convert to date number. This is a single number
% that specifies the date
-17:17 % Push [-17 -16 ... 16 17]
+ % Add to date number, element-wise. This gives a row vector of 35
% date numbers centered around the input date
t % Duplicate
7: % Push [1 2 ... 7]
) % Index into the 35-element vector. This keeps the first 7 entries
8XO % Convert to day-of-week in 3 letters. Gives a 3-col char matrix
O3Z( % Write char 0 (equivalent to space for display purposes) into the
% 3rd column
!1e % Tranpose and linearize into a row. This produces a string such as
% 'Tu We Th Fr Sa Su Mo ', to be used as column headings
w % Swap. This brings to top the row vector of 35 date numbers
% computed from the input
7XO % Convert to day-of-month. Gives a 2-col char matrix
U % Convert each row to number
7e! % Reshape into 7-row matrix and transpose
% Implicit display. This prints the string with the headings and
% the matrix. The latter has a minimum-one-space separation between
% columns, so it is aligned with the headings
$endgroup$
add a comment |
$begingroup$
MATL, 34 33 31 bytes
YO-17:17+t7:)8XOO3Z(!1ew7XOU7e!
Try it online!
Explanation
YO % Implicit input. Convert to date number. This is a single number
% that specifies the date
-17:17 % Push [-17 -16 ... 16 17]
+ % Add to date number, element-wise. This gives a row vector of 35
% date numbers centered around the input date
t % Duplicate
7: % Push [1 2 ... 7]
) % Index into the 35-element vector. This keeps the first 7 entries
8XO % Convert to day-of-week in 3 letters. Gives a 3-col char matrix
O3Z( % Write char 0 (equivalent to space for display purposes) into the
% 3rd column
!1e % Tranpose and linearize into a row. This produces a string such as
% 'Tu We Th Fr Sa Su Mo ', to be used as column headings
w % Swap. This brings to top the row vector of 35 date numbers
% computed from the input
7XO % Convert to day-of-month. Gives a 2-col char matrix
U % Convert each row to number
7e! % Reshape into 7-row matrix and transpose
% Implicit display. This prints the string with the headings and
% the matrix. The latter has a minimum-one-space separation between
% columns, so it is aligned with the headings
$endgroup$
add a comment |
$begingroup$
MATL, 34 33 31 bytes
YO-17:17+t7:)8XOO3Z(!1ew7XOU7e!
Try it online!
Explanation
YO % Implicit input. Convert to date number. This is a single number
% that specifies the date
-17:17 % Push [-17 -16 ... 16 17]
+ % Add to date number, element-wise. This gives a row vector of 35
% date numbers centered around the input date
t % Duplicate
7: % Push [1 2 ... 7]
) % Index into the 35-element vector. This keeps the first 7 entries
8XO % Convert to day-of-week in 3 letters. Gives a 3-col char matrix
O3Z( % Write char 0 (equivalent to space for display purposes) into the
% 3rd column
!1e % Tranpose and linearize into a row. This produces a string such as
% 'Tu We Th Fr Sa Su Mo ', to be used as column headings
w % Swap. This brings to top the row vector of 35 date numbers
% computed from the input
7XO % Convert to day-of-month. Gives a 2-col char matrix
U % Convert each row to number
7e! % Reshape into 7-row matrix and transpose
% Implicit display. This prints the string with the headings and
% the matrix. The latter has a minimum-one-space separation between
% columns, so it is aligned with the headings
$endgroup$
MATL, 34 33 31 bytes
YO-17:17+t7:)8XOO3Z(!1ew7XOU7e!
Try it online!
Explanation
YO % Implicit input. Convert to date number. This is a single number
% that specifies the date
-17:17 % Push [-17 -16 ... 16 17]
+ % Add to date number, element-wise. This gives a row vector of 35
% date numbers centered around the input date
t % Duplicate
7: % Push [1 2 ... 7]
) % Index into the 35-element vector. This keeps the first 7 entries
8XO % Convert to day-of-week in 3 letters. Gives a 3-col char matrix
O3Z( % Write char 0 (equivalent to space for display purposes) into the
% 3rd column
!1e % Tranpose and linearize into a row. This produces a string such as
% 'Tu We Th Fr Sa Su Mo ', to be used as column headings
w % Swap. This brings to top the row vector of 35 date numbers
% computed from the input
7XO % Convert to day-of-month. Gives a 2-col char matrix
U % Convert each row to number
7e! % Reshape into 7-row matrix and transpose
% Implicit display. This prints the string with the headings and
% the matrix. The latter has a minimum-one-space separation between
% columns, so it is aligned with the headings
edited Apr 5 at 12:01
answered Apr 5 at 8:53
Luis MendoLuis Mendo
75.3k889292
75.3k889292
add a comment |
add a comment |
$begingroup$
Java (JDK), 149 bytes
d->{d.add(5,-24);for(int i=0,w;i<42;d.add(5,1))System.out.printf("%c%2s",i%7<1?10:32,i++<7?"SaSuMoTuWeThFr".substring(w=d.get(7)%7*2,w+2):d.get(5));}
Try it online!
Credits
- -13 bytes thanks to Kevin Cruijssen.
$endgroup$
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
$begingroup$
Maybe you see something more to combine thei
andj
somehow? Or something shorter forj++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does thew
stand for? Why noth
for header?
$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
$begingroup$
@KevinCruijssenw
for "day of week". Also, bit twiddling can only lead to(i%7+6)/7*22+10
which is much longer.
$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
|
show 5 more comments
$begingroup$
Java (JDK), 149 bytes
d->{d.add(5,-24);for(int i=0,w;i<42;d.add(5,1))System.out.printf("%c%2s",i%7<1?10:32,i++<7?"SaSuMoTuWeThFr".substring(w=d.get(7)%7*2,w+2):d.get(5));}
Try it online!
Credits
- -13 bytes thanks to Kevin Cruijssen.
$endgroup$
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
$begingroup$
Maybe you see something more to combine thei
andj
somehow? Or something shorter forj++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does thew
stand for? Why noth
for header?
$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
$begingroup$
@KevinCruijssenw
for "day of week". Also, bit twiddling can only lead to(i%7+6)/7*22+10
which is much longer.
$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
|
show 5 more comments
$begingroup$
Java (JDK), 149 bytes
d->{d.add(5,-24);for(int i=0,w;i<42;d.add(5,1))System.out.printf("%c%2s",i%7<1?10:32,i++<7?"SaSuMoTuWeThFr".substring(w=d.get(7)%7*2,w+2):d.get(5));}
Try it online!
Credits
- -13 bytes thanks to Kevin Cruijssen.
$endgroup$
Java (JDK), 149 bytes
d->{d.add(5,-24);for(int i=0,w;i<42;d.add(5,1))System.out.printf("%c%2s",i%7<1?10:32,i++<7?"SaSuMoTuWeThFr".substring(w=d.get(7)%7*2,w+2):d.get(5));}
Try it online!
Credits
- -13 bytes thanks to Kevin Cruijssen.
edited Apr 5 at 13:22
answered Apr 5 at 7:40
Olivier GrégoireOlivier Grégoire
9,39511944
9,39511944
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
$begingroup$
Maybe you see something more to combine thei
andj
somehow? Or something shorter forj++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does thew
stand for? Why noth
for header?
$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
$begingroup$
@KevinCruijssenw
for "day of week". Also, bit twiddling can only lead to(i%7+6)/7*22+10
which is much longer.
$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
|
show 5 more comments
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
$begingroup$
Maybe you see something more to combine thei
andj
somehow? Or something shorter forj++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does thew
stand for? Why noth
for header?
$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
$begingroup$
@KevinCruijssenw
for "day of week". Also, bit twiddling can only lead to(i%7+6)/7*22+10
which is much longer.
$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
1
1
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
$begingroup$
159 bytes
$endgroup$
– Kevin Cruijssen
Apr 5 at 12:49
1
1
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
$begingroup$
@KevinCruijssen Wait... what? I congratulate you! I tried to do this, but couldn't find a way to do it, and yet you did it! Very nice :-)
$endgroup$
– Olivier Grégoire
Apr 5 at 12:55
1
1
$begingroup$
Maybe you see something more to combine the
i
and j
somehow? Or something shorter for j++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
$begingroup$
Maybe you see something more to combine the
i
and j
somehow? Or something shorter for j++%7<1?10:32
with some bitwise magic? But I'll leave that to you. I'm going back to work, lol. ;)$endgroup$
– Kevin Cruijssen
Apr 5 at 12:59
1
1
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does the
w
stand for? Why not h
for header?$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
$begingroup$
Ah, of course.. Nice teamwork! ;) PS: Where does the
w
stand for? Why not h
for header?$endgroup$
– Kevin Cruijssen
Apr 5 at 13:11
1
1
$begingroup$
@KevinCruijssen
w
for "day of week". Also, bit twiddling can only lead to (i%7+6)/7*22+10
which is much longer.$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
$begingroup$
@KevinCruijssen
w
for "day of week". Also, bit twiddling can only lead to (i%7+6)/7*22+10
which is much longer.$endgroup$
– Olivier Grégoire
Apr 5 at 13:13
|
show 5 more comments
$begingroup$
PHP, 197 189 187 bytes
for($d=date_create($argn)->sub($i=new DateInterval(P17D)),$i->d=1;$x++<35;$h.=$x<8?substr($d->format(D),0,2).' ':'',$d->add($i))$o.=str_pad($d->format(j),3,' ',2);echo wordwrap($h.$o,20);
Try it online!
Input is STDIN
as a date string. Run with php -nF
.
$ echo April 2 2019|php -nF cal.php
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
Verify all test cases
Or 174 bytes with zero-padded single digits.
$endgroup$
add a comment |
$begingroup$
PHP, 197 189 187 bytes
for($d=date_create($argn)->sub($i=new DateInterval(P17D)),$i->d=1;$x++<35;$h.=$x<8?substr($d->format(D),0,2).' ':'',$d->add($i))$o.=str_pad($d->format(j),3,' ',2);echo wordwrap($h.$o,20);
Try it online!
Input is STDIN
as a date string. Run with php -nF
.
$ echo April 2 2019|php -nF cal.php
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
Verify all test cases
Or 174 bytes with zero-padded single digits.
$endgroup$
add a comment |
$begingroup$
PHP, 197 189 187 bytes
for($d=date_create($argn)->sub($i=new DateInterval(P17D)),$i->d=1;$x++<35;$h.=$x<8?substr($d->format(D),0,2).' ':'',$d->add($i))$o.=str_pad($d->format(j),3,' ',2);echo wordwrap($h.$o,20);
Try it online!
Input is STDIN
as a date string. Run with php -nF
.
$ echo April 2 2019|php -nF cal.php
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
Verify all test cases
Or 174 bytes with zero-padded single digits.
$endgroup$
PHP, 197 189 187 bytes
for($d=date_create($argn)->sub($i=new DateInterval(P17D)),$i->d=1;$x++<35;$h.=$x<8?substr($d->format(D),0,2).' ':'',$d->add($i))$o.=str_pad($d->format(j),3,' ',2);echo wordwrap($h.$o,20);
Try it online!
Input is STDIN
as a date string. Run with php -nF
.
$ echo April 2 2019|php -nF cal.php
Sa Su Mo Tu We Th Fr
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
Verify all test cases
Or 174 bytes with zero-padded single digits.
edited Apr 5 at 16:44
answered Apr 4 at 21:11
gwaughgwaugh
2,0931518
2,0931518
add a comment |
add a comment |
$begingroup$
Red, 153 131 bytes
func[d][s: d - 24 loop 7[prin[""copy/part system/locale/days/(s/10) 2]s:
s + 1]loop 5[print""loop 7[prin pad/left s/4 3 s: s + 1]]]
Try it online!
$endgroup$
add a comment |
$begingroup$
Red, 153 131 bytes
func[d][s: d - 24 loop 7[prin[""copy/part system/locale/days/(s/10) 2]s:
s + 1]loop 5[print""loop 7[prin pad/left s/4 3 s: s + 1]]]
Try it online!
$endgroup$
add a comment |
$begingroup$
Red, 153 131 bytes
func[d][s: d - 24 loop 7[prin[""copy/part system/locale/days/(s/10) 2]s:
s + 1]loop 5[print""loop 7[prin pad/left s/4 3 s: s + 1]]]
Try it online!
$endgroup$
Red, 153 131 bytes
func[d][s: d - 24 loop 7[prin[""copy/part system/locale/days/(s/10) 2]s:
s + 1]loop 5[print""loop 7[prin pad/left s/4 3 s: s + 1]]]
Try it online!
edited Apr 5 at 8:02
answered Apr 5 at 7:45
Galen IvanovGalen Ivanov
7,44211034
7,44211034
add a comment |
add a comment |
$begingroup$
T-SQL, 203 bytes
DECLARE @f date='2020-02-19'
,@ char(20)=0,@d char(105)=0SELECT
@=left(format(d,'D'),2)+' '+@,@d=right(d,2)+char(32-n%7/6*19)+@d
FROM(SELECT dateadd(d,number-17,@f)d,number n
FROM spt_values WHERE'P'=type)x ORDER BY-n
PRINT @+'
'+@d
The online version is slightly different, this posted version works in MS-SQL Studio Management. It saves 1 bytes compared with the online version, but doesn't give the correct result online
Try it online
$endgroup$
add a comment |
$begingroup$
T-SQL, 203 bytes
DECLARE @f date='2020-02-19'
,@ char(20)=0,@d char(105)=0SELECT
@=left(format(d,'D'),2)+' '+@,@d=right(d,2)+char(32-n%7/6*19)+@d
FROM(SELECT dateadd(d,number-17,@f)d,number n
FROM spt_values WHERE'P'=type)x ORDER BY-n
PRINT @+'
'+@d
The online version is slightly different, this posted version works in MS-SQL Studio Management. It saves 1 bytes compared with the online version, but doesn't give the correct result online
Try it online
$endgroup$
add a comment |
$begingroup$
T-SQL, 203 bytes
DECLARE @f date='2020-02-19'
,@ char(20)=0,@d char(105)=0SELECT
@=left(format(d,'D'),2)+' '+@,@d=right(d,2)+char(32-n%7/6*19)+@d
FROM(SELECT dateadd(d,number-17,@f)d,number n
FROM spt_values WHERE'P'=type)x ORDER BY-n
PRINT @+'
'+@d
The online version is slightly different, this posted version works in MS-SQL Studio Management. It saves 1 bytes compared with the online version, but doesn't give the correct result online
Try it online
$endgroup$
T-SQL, 203 bytes
DECLARE @f date='2020-02-19'
,@ char(20)=0,@d char(105)=0SELECT
@=left(format(d,'D'),2)+' '+@,@d=right(d,2)+char(32-n%7/6*19)+@d
FROM(SELECT dateadd(d,number-17,@f)d,number n
FROM spt_values WHERE'P'=type)x ORDER BY-n
PRINT @+'
'+@d
The online version is slightly different, this posted version works in MS-SQL Studio Management. It saves 1 bytes compared with the online version, but doesn't give the correct result online
Try it online
edited Apr 5 at 11:04
answered Apr 5 at 9:14
t-clausen.dkt-clausen.dk
2,084314
2,084314
add a comment |
add a comment |
$begingroup$
Python 2, 115 bytes
from datetime import*
d=input()
for i in range(42):print(d+timedelta(i-24)).strftime('%'+'da'[i<7])[:2]+i%7/6*'n',
Try it online!
Not sure if this is allowed... takes input from STDIN in the form date(year, month, day)
. This can also be represented as __import__('datetime').date(year, month, day)
. These are really __import__('datetime').date
objects.
$endgroup$
add a comment |
$begingroup$
Python 2, 115 bytes
from datetime import*
d=input()
for i in range(42):print(d+timedelta(i-24)).strftime('%'+'da'[i<7])[:2]+i%7/6*'n',
Try it online!
Not sure if this is allowed... takes input from STDIN in the form date(year, month, day)
. This can also be represented as __import__('datetime').date(year, month, day)
. These are really __import__('datetime').date
objects.
$endgroup$
add a comment |
$begingroup$
Python 2, 115 bytes
from datetime import*
d=input()
for i in range(42):print(d+timedelta(i-24)).strftime('%'+'da'[i<7])[:2]+i%7/6*'n',
Try it online!
Not sure if this is allowed... takes input from STDIN in the form date(year, month, day)
. This can also be represented as __import__('datetime').date(year, month, day)
. These are really __import__('datetime').date
objects.
$endgroup$
Python 2, 115 bytes
from datetime import*
d=input()
for i in range(42):print(d+timedelta(i-24)).strftime('%'+'da'[i<7])[:2]+i%7/6*'n',
Try it online!
Not sure if this is allowed... takes input from STDIN in the form date(year, month, day)
. This can also be represented as __import__('datetime').date(year, month, day)
. These are really __import__('datetime').date
objects.
answered Apr 6 at 19:14
Erik the OutgolferErik the Outgolfer
33k429106
33k429106
add a comment |
add a comment |
$begingroup$
Excel VBA, 190 bytes
Function z(i)
Dim d(5, 6)
v = DateValue(i) - 17
For x = 1 To 5
For y = 0 To 6
d(0, y) = Left(WeekdayName(Weekday(v + y)), 2)
d(x, y) = day(v + y + (x - 1) * 7)
Next
Next
z = d()
End Function
Takes input in the form of a valid date string for Excel VBA (e.g. February 19, 2020; 2/19/2020; 19-Feb-2019), and returns an array with the given calendar centered on it.
$endgroup$
add a comment |
$begingroup$
Excel VBA, 190 bytes
Function z(i)
Dim d(5, 6)
v = DateValue(i) - 17
For x = 1 To 5
For y = 0 To 6
d(0, y) = Left(WeekdayName(Weekday(v + y)), 2)
d(x, y) = day(v + y + (x - 1) * 7)
Next
Next
z = d()
End Function
Takes input in the form of a valid date string for Excel VBA (e.g. February 19, 2020; 2/19/2020; 19-Feb-2019), and returns an array with the given calendar centered on it.
$endgroup$
add a comment |
$begingroup$
Excel VBA, 190 bytes
Function z(i)
Dim d(5, 6)
v = DateValue(i) - 17
For x = 1 To 5
For y = 0 To 6
d(0, y) = Left(WeekdayName(Weekday(v + y)), 2)
d(x, y) = day(v + y + (x - 1) * 7)
Next
Next
z = d()
End Function
Takes input in the form of a valid date string for Excel VBA (e.g. February 19, 2020; 2/19/2020; 19-Feb-2019), and returns an array with the given calendar centered on it.
$endgroup$
Excel VBA, 190 bytes
Function z(i)
Dim d(5, 6)
v = DateValue(i) - 17
For x = 1 To 5
For y = 0 To 6
d(0, y) = Left(WeekdayName(Weekday(v + y)), 2)
d(x, y) = day(v + y + (x - 1) * 7)
Next
Next
z = d()
End Function
Takes input in the form of a valid date string for Excel VBA (e.g. February 19, 2020; 2/19/2020; 19-Feb-2019), and returns an array with the given calendar centered on it.
answered 9 hours ago
william porterwilliam porter
1415
1415
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182681%2ftoday-is-the-center%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown