01. 지정한 날짜의 x요일은 몇월몇일 일까? (inputDay 0=Sunday, 1=Monday, ...)

function getDateByDayOfWeek(d, inputDay) {
	d = new Date(d);
	var day = d.getDay(),
	diff = d.getDate() - day + (day == 0 ? -6:inputDay); // adjust when day is sunday
	return new Date(d.setDate(diff));
}

// 2020년05월26일의 수요일은 몇년몇월몇일 일까?
getDayOfWeek(new Date("2020-05-26"), 3);
>> Wed May 27 2020 09:00:00 GMT+0900 (대한민국 표준시)

 

02. 지정한 요일에 따른 주차구하는 함수 (해당 주차 / 해당주차 시작날짜 / 해당주차 끝나는날짜를 리턴)

function getWeekInfo(customDate, weekStand) {
	// 기준주차(시작일) 월 화 수 목 금 토 일.
	// 오늘날짜 구하기(일주일 +7)
	var date = new Date(customDate);
	var week = new Array('일', '월', '화', '수', '목', '금', '토');

	// 월요일을 중심으로한 주차 구하기(js 기준 : 일요일 0 월요일 1 ~ 토요일 6)
	// 첫날 마지막날 구하기(현재월)
	var firstDay,
	lastDay;
	firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
	lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

	//Test 2017.01월 기준.
	//firstDay = new Date(date.getFullYear(), date.getMonth()-1,1);
	//lastDay = new Date(date.getFullYear(), date.getMonth() , 0);

	var weekObj = null;
	var weekObjArray = new Array();
	var weekStandTemp = weekStand;
	var firstWeekEndDate = true;
	var thisMonthFirstWeek = firstDay.getDay();

	//시작일과 시작주일이 같은경우.
	if (firstDay.getDay() == weekStand) {
		for (var i = 1; i <= 6; i++) {
			if (lastDay.getMonth() < firstDay.getMonth()) {
				break;
			}
			weekObj = new Object();
			//console.log(i + "주차");
			//console.log(firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString());
			//console.log("~");
			weekObj.weekNum = i;
			weekObj.weekStartDate = firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString();
			firstDay.setDate(firstDay.getDate() + 6);
			if (lastDay.getMonth() < firstDay.getMonth()) {
				firstDay.setMonth(firstDay.getMonth() - 1);
				firstDay.setDate(lastDay.getDate());
				weekObj.weekEndDate = firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString();
				weekObjArray.push(weekObj);
				break;
			}

			weekObj.weekEndDate = firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString();
			weekObjArray.push(weekObj);
			//console.log(firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString());
			firstDay.setDate(firstDay.getDate() + 1);

		}
	} else {
		//시작일과 시작주일이 다른경우.
		for (var i = 1; i <= 6; i++) {
			if (lastDay.getMonth() < firstDay.getMonth()) {
				break;
			}
			weekObj = new Object();
			//console.log(i + "주차");
			//console.log(firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString());
			//console.log("~");
			weekObj.weekNum = i;
			weekObj.weekStartDate = firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString();
			if (weekStand > thisMonthFirstWeek) {

				if (firstWeekEndDate) {
					if ((weekStand - firstDay.getDay()) == 1) {
						firstDay.setDate(firstDay.getDate() + (weekStand - firstDay.getDay()) - 1);
					}
					if ((weekStand - firstDay.getDay()) > 1) {
						firstDay.setDate(firstDay.getDate() + (weekStand - firstDay.getDay()) - 1)
					}
					firstWeekEndDate = false;
				} else {
					firstDay.setDate(firstDay.getDate() + 6);
				}

			} else {
				firstDay.setDate(firstDay.getDate() + (6 - firstDay.getDay()) + weekStand);
			}

			if (lastDay.getMonth() < firstDay.getMonth()) {
				firstDay.setMonth(firstDay.getMonth() - 1);
				firstDay.setDate(lastDay.getDate());
			}
			weekObj.weekEndDate = firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString();
			weekObjArray.push(weekObj);
			//console.log(firstDay.getFullYear().toString() + "-" + (firstDay.getMonth() + 1).toString() + "-" + firstDay.getDate().toString());
			firstDay.setDate(firstDay.getDate() + 1);
		}
	}
	return weekObjArray;
}

+ Recent posts