//get a preferred day of a month
var getGivenDateOfMonth = function (startDate, dayOfWeek, weekNumber) {
var startOfMonth = moment(startDate).utc().startOf('month').startOf('isoweek');
var dayOne = moment((moment(startDate, "YYYY-MM-DD").format("YYYY-MM") + "-01"),"YYYY-MM-DD");
var studyDate;
if (dayOne.isoWeekday() === 1) {
studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
.add(weekNumber, 'w');
}
else if (dayOne.isoWeekday() <= dayOfWeek) {
studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
.add(weekNumber - 1, 'w');
} else {
studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
.add(weekNumber, 'w');
}
if (studyDate.month() == startOfMonth.month()) {
studyDate = studyDate.subtract(1, 'w');
}
return studyDate;
};
startDate is the date in a given month.
dayOfWeek is the moment js ISO day of week and
weeknumber is the week number(1,2,3,4) you want