posted by 지긋이 2020. 1. 14. 16:02

오늘 기준일이 2020.02.17일이라고 할 때

var _today1 = new Date();
var _today2 = new Date();
var firDay, lasDay;

_today1 = new Date(_today1.getFullYear(), _today1.getMonth()-5,1); //6개월전(해당월 포함)

firDay = dayFormatChk(_today1, 1); //6개월전 Format 변경
lasDay = dayFormatChk(_today2, 2); //sysdate 말일

$("#sFromYmd").val(firDay);
$("#sToYmd").val(lasDay);

console.log("todayFormat::"+firDay+"\n"+lasDay);
=> 출력

todayFormat::2019-09-01
2020-02-29

function dayFormatChk(_today, type){
var day;
if(type==1){//현재 6개월 전 구하기
day = _today.getFullYear()+"-"+("0"+(_today.getMonth()+1)).slice(-2)+"-"
+("0"+(_today.getDate())).slice(-2); 

}else if(type==2){ //현재 말일 구하기
day = new Date(_today.getFullYear(), _today.getMonth()+1,0);

day = _today.getFullYear()+"-"+("0"+(day.getMonth()+1)).slice(-2)+"-"
+("0"+(day.getDate())).slice(-2);
}
return day;

}