Plan your Sundays!

JavaScript (ES6), 107 bytes

f=
(y,m,a=[...Array(32)].map((_,i)=>new Date(y,m-1,i)).filter(d=>d.getMonth()==m-1&!d.getDay()))=>[a.length,a]
<div oninput=o.textContent=[].concat(...f(y.value,m.value)).map((d,i)=&gt;i?d.toDateString():d).join`\n`><input id=y type=number min=1000 max=9999 value=2017><input id=m type=number min=1 max=12><pre id=o>

Edit: Adding an explicit count cost 15 bytes. Formatting the output would cost at least another 33 bytes depending on how strict the output format is.


Python 2, 150,148,145 bytes

from calendar import*
y,m=map(int,input().split())
z=filter(None,zip(*monthcalendar(y,m))[6])
print len(z)
for i in z:print'%02d-%02d-%s'%(i,m,y)

Try it online!

-3 bytes making stuff more pythonic (using zip and filter!)


PowerShell, 91 bytes

param($y,$m)($a=(1..31|%{Date "$m/$_/$y"}|?{!$_.dayofweek})).count;$a|%{"{0:d-M-yyyy}"-f$_}

Try it online!

(Special note - this is locale and culture settings dependent. Since TIO is running as en-us, it works correctly there as-is. This code may need to be changed for different locales.)

Takes input as two integers $y and $m. Loops from 1 to 31, getting a new datetime object for each possible date (via the Get-Date cmdlet). Will toss errors to STDERR (ignored by default on code-golf challenges) for months with fewer than 31 days, but that doesn't affect output. We take each of those datetime objects and use a Where-Object (|?{...}) on them, with the clause of !$_.dayofweek. The property .dayofweek is a number from 0 to 6, with 0 conveniently corresponding to Sunday, so the ! of that is truthy, which saves a couple bytes compared to an equality check like -eq0.

The Sundays are then gathered in parens and stored into $a. We then take the .count thereof, and that's placed on the pipeline. Next, we loop through $a and use the -format operator to construct the correct output format. Note that this doesn't output leading zeros for days or months. Those date strings are also left on the pipeline, and an implicit Write-Output at program completion prints them with newline separators.


NB - if the output format was more flexible, we could just leave $a on the pipeline and not need to loop through it. That will stringify the datetime objects as the long-date format including time information, but gets us down to 69 bytes, which would (currently) only be beaten by Mathematica and MATL.

Tags:

Date

Code Golf