Created
April 20, 2019 18:07
-
-
Save LukeMwila/d5eb12d892e92ab49d72f8b5761479c5 to your computer and use it in GitHub Desktop.
Function that returns an array of date objects for a particular week, given a certain date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getDaysOfWeekFromGivenDate = ( | |
date: Date | null | |
) => { | |
if (date) { | |
const startOfWeek = moment(date).startOf('isoWeek'); | |
const weekArray = moment.weekdays(); | |
const daysOfWeekInSelectedDate = daysOfWeek.map((d, i) => { | |
return startOfWeek | |
.clone() | |
.add(i, 'd') | |
.toDate(); | |
}); | |
return daysOfWeekInSelectedDate; | |
} else { | |
return []; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line 7,
daysOfWeek
would be undefined, shouldn't it beweekArray
instead?