:::: 개발 ::::/::: JSCRIPT :::

날짜 기간별 조회 한달 단위 js script

nayha 2017. 4. 14. 14:11

$(document).ready(function(){

 //기준 날짜 셋팅 yyyyMMDD 형식으로 셋팅

var settingDate = new Date();

settingDate.setDate(settingDate.getDate()); //오늘

//console.log( dateToYYYYMMDD(settingDate) );

var Today = dateToYYYYMMDD(settingDate);


settingDate.setMonth(settingDate.getMonth()-1); //1달 전

//console.log( dateToYYYYMMDD(settingDate) );

var oneMonth = dateToYYYYMMDD(settingDate);

 

settingDate.setMonth(settingDate.getMonth()-3); //3달 전

//console.log( dateToYYYYMMDD(settingDate) );

var threeMonth = dateToYYYYMMDD(settingDate);

 

settingDate.setMonth(settingDate.getMonth()-6); //6달 전

//console.log( dateToYYYYMMDD(settingDate) );

var sixMonth = dateToYYYYMMDD(settingDate);


//기간조회 클릭할때 이벤트..

$('.select_wrap li').click( function(){

  var index = $("li").index(this);

   //li 4번째가 1달

 switch (index) { 

case 4: 

 settingDate.setMonth(settingDate.getMonth()-1); 

$('.icon_sort').text( oneMonth +"-"+ Today );

$("#start_date").val(oneMonth ); 

      $("#end_date").val(Today );

break;

case 5: 

$('.icon_sort').text( threeMonth +"-"+ Today );

$("#start_date").val(threeMonth ); 

      $("#end_date").val(Today );

break;

case 6: 

$('.icon_sort').text( sixMonth +"-"+ Today );

$("#start_date").val(sixMonth ); 

      $("#end_date").val(Today );

break;

default:

$('.icon_sort').text( oneMonth +"-"+ Today );

$("#start_date").val(oneMonth ); 

      $("#end_date").val(Today );

//alert('Nobody Wins!');

}   


}); //기간조회 클릭할때 이벤트..END

});

function dateToYYYYMMDD(date){

   function pad(num) {

       num = num + '';

       return num.length < 2 ? '0' + num : num;

   }

   return date.getFullYear() + '-' + pad(date.getMonth()+1) + '-' + pad(date.getDate());

}

반응형