반응형
// EX) fnSetWeekDate("20201202","1week");

function fnSetWeekDate(vNow, vDate) 
{
	var vPrevDate;
	//var vNowDate = new Date().getFullYear() + "-" + _pad(new Date().getMonth() + 1, 2) + "-" + + _pad(new Date().getDate(), 2);

	var vYYYY = vNow.substring(0, 4);
	var vMM = vNow.substring(4, 6);
	var vDD = vNow.substring(6, 8);

	var d = new Date(vMM+"/"+vDD+"/"+vYYYY);
	var dayOfMonth = d.getDate();
	var monthOfYear = d.getMonth();

	if (vDate == "1week") 
	{
		d.setDate(dayOfMonth + 7);
		vPrevDate = d.getFullYear() + _pad(d.getMonth() + 1, 2) + _pad(d.getDate(), 2);
	}
	else if (vDate == "1month") 
	{
		d.setMonth(monthOfYear + 1);
		vPrevDate = d.getFullYear() + _pad(d.getMonth() + 1, 2) + _pad(d.getDate(), 2);
	}
	else if (vDate == "3month") 
	{
		d.setMonth(monthOfYear + 3);
		vPrevDate = d.getFullYear() + _pad(d.getMonth() + 1, 2) + _pad(d.getDate(), 2);
	}

	return vPrevDate;
}

 

_pad 함수

//숫자 width만큼 앞에 0 붙혀주는 함수 EX) widht = 2일떄 1은 01로 찍힘
function _pad(n, width) {
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
}

+ Recent posts