Skip to content

Instantly share code, notes, and snippets.

@LukeMwila
Created April 20, 2019 18:07
Show Gist options
  • Save LukeMwila/d5eb12d892e92ab49d72f8b5761479c5 to your computer and use it in GitHub Desktop.
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
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 [];
}
};
@Sensational-Code
Copy link

On line 7, daysOfWeek would be undefined, shouldn't it be weekArray instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment