These Days Work for Me

Perl 6, 47 bytes

{my$d=Date.new($_);map($d+*-$d.day-of-week,^7)}

Try it online!

The code is almost self-explanatory. First we make up a Date object $d from the string, which happens to be in the nice ISOwhatever format. We can perform arithmetic with the dates (adding an integer = adding that many days, the same with subtracting). So $d-$d.day-of-week is the last this (oh my god, week has always started with Monday for me :D) week's Sunday. (.day-of-week is 1 for Monday to 7 for Sunday.) We then map over 0 to 6 (^7) and add that to the Sunday, getting all the days in the week. The number we're mapping over appears in the place of the star *. This list is then returned.


JavaScript (ES6), 73 63 62 Bytes

I thought I'd also have a go at it in JavaScript.

Edit: Using the milliseconds approach, as allowed by the prompt, I reduced it down to 62 bytes.

s=>[..."0123456"].map(i=>(x=new Date(s))-(x.getDay()-i)*864e5)

You could perform a .map(x=>new Date(x)) on the returned array if you wanted to convert it.


Maintaining the date approach (72 Bytes)

s=>[..."0123456"].map(i=>new Date((x=new Date(s))-(x.getDay()-i)*864e5))

There was a bug in my initial submission, it is fixed. Thanks to an observation by milk, I was able to remove the bug and still lower the byte count.


JavaScript (ES6), 76 bytes

I think I've finally found the environment least conducive to efficient golfing: from behind my bar, while serving pints with my other hand!

s=>[...""+8e6].map((_,x)=>d.setDate(d.getDate()-d.getDay()+x),d=new Date(s))

Try it online (Snippet to follow)


Explanation

s=>

Anonymous function taking the string (of format yyyy-(m)m-(d)d) as an argument via parameter s.

[...""+8e6]

Create an array of 7 elements by converting 8000000 in scientific notation to a string and destructuring it, which is shorter than [...Array(7)].

.map((_,x)=>          )

Map over the array, passing each element through a function where _ is the current element, which won't be used, and x is the current index, 0-based.

,d=new Date(s)

Convert s to a Date object and assign it to variable d

d.setDate(     )

Set the date of d.

d.getDate()

Get the current date of d.

-d.getDay()

Subtract the current weekday number, 0-indexed, to get Sunday.

+x

Add x.

Tags:

Date

Code Golf