본문 바로가기
웹프로그래밍/Javascript|JQuery|Jsp

일정 기간 버튼 선택(오늘,어제,1주,1개월,3개월,6개월,1년)

by Seras 2017. 6. 19.
반응형






[JavaScript,JSP]

일정 기간 버튼 선택하면 날짜 세팅

(오늘,어제,1주,1개월,3개월,6개월,1년)




Controller

if (searchVO.getSearchYear()==null || searchVO.getSearchYear().equals("")){

if(searchVO.getSearchDtFrom()!=null){

//사용기간 시작 범위 Date 형식 변환

SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd");

Date StartRangeDate =  transFormat.parse(searchVO.getSearchDtFrom());

Date EndRangeDate = transFormat.parse(searchVO.getSearchDtTo());

String Start1 = transFormat.format(StartRangeDate);

String End1 = transFormat.format(EndRangeDate);

//사용기간 시작~종료 값 

searchVO.setSearchDtFrom(Start1);

searchVO.setSearchDtTo(End1);

//사용기간 시작 범위 연도만 뽑아줌

SimpleDateFormat transFormatY = new SimpleDateFormat("yyyy");

//사용기간 시작 범위 연도

String startY = transFormatY.format(StartRangeDate);

//연도는 시작 범위 DATE 년도로 셋팅함

searchVO.setSearchYear(startY);

}else{

searchVO.setSearchYear( Calendar.getInstance().get(Calendar.YEAR) + "" );

}

}

 

 



VO

private String searchDtFrom, searchDtTo; //검색 시작 종료일

private String searchYear; //연도


private String searchStartYearOne; //시작범위 검색 월

private String searchStartMonthOne; //시작범위 검색 월

private String searchStartDayOne; //시작범위 검색 일

private String searchEndYearOne; //시작범위 검색 월

private String searchEndMonthOne; //종료범위 검색 월

private String searchEndDayOne; //종료범위 검색 일

private String searchMonth; //월

private String searchStartDate; //시작날짜

private String searchEndDate; //종료날짜

private String searchDate; //날짜

private String thisYear; //현재연도

private String thisMonth; //현재월

private String searchPeriod; //기간



JSP


<tr>

<th scope="row">기간</th>

<td colspan="6"> 

<form:input path="searchDtFrom" cssClass="datepicker onlyDate essential"

                   title="검색시작일"enddate="searchEndDate" readonly="true" />

~

                 <form:input path="searchDtTo" cssClass="datepicker onlyDate essential" title="검색종료일" 

                       startdate="searchStartDate" readonly="true" />

<span class="btn_pack type10"><button type="button" name="search" class="date_range d0">오늘</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range d-1">어제</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range d-7">1주</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range m-1">1개월</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range m-3">3개월</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range m-6">6개월</button></span>

<span class="btn_pack type10"><button type="button" name="search" class="date_range y-1">1년</button></span>


</td>

</tr> 



javascript


function dateAdd(src, field, amount){

if(isNaN(amount)){

return null;

}


var d = (src.getDate ? src : toDate(src, '-'));


amount = new Number(amount);


switch(field.toLowerCase()){

case 'y': {

d.setFullYear(d.getFullYear() + amount);

break;

}

case 'm': {

d.setMonth(d.getMonth() + amount);

break;

}

case 'd': {

d.setDate(d.getDate() + amount);

break;

}

default: {

return null;

}

}


var month = parseInt(d.getMonth()) + 1;

var day = parseInt(d.getDate());


return d.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);

};



function formatDate(d, f) {

    if (!this.valueOf()) return ' ';


    return f.replace(/(yyyy|mm|dd|hh|nn|ss|a\/p)/gi, function($1){

switch ($1.toLowerCase()){

case 'yyyy': return d.getFullYear();

case 'mm':   return (d.getMonth() + 1 < 10 ? '0' : '') + (d.getMonth() + 1);

case 'dd':   return (d.getDate() < 10 ? '0' : '') + d.getDate();

case 'hh':   return (d.getHours() < 10 ? '0' : '') + d.getHours();

case 'nn':   return (d.geMinutes() < 10 ? '0' : '') + d.geMinutes();

case 'ss':   return (d.getSeconds() < 10 ? '0' : '') + d.getSeconds();

}

});

};



function toDate(dateStr, delm) {

var year;

var month;

var day;


if(delm){

var d = dateStr.split(delm);

year = d[0];

month = d[1] - 1;

day = d[2];

}else{

year = dateStr.substr(0, 4);

month = dateStr.substr(4, 2) - 1;

day = dateStr.substr(6, 2);

}


return new Date(year, month, day);

}; 


반응형