상세 컨텐츠

본문 제목

자바스크립트 상세 달력버튼 만들기..

IT 세상

by 이현민 (지후지율아빠) 2008. 1. 15. 10:03

본문

전체, 1개월,1주일,3개월,6개월,1년,2년...
해서 달력 초기값 세팅하는 것 만들어 봤는데,
좀 지저분하군요..


//상세검색 달력세팅
function setCalendar(val,mode){
 var form = document.search;
 
 if (val=='all')
 {
  form.fromdate.value="";
  form.todate.value="";
 }else {


 
  var year = "";
  var month = "";
  var day = "";
  var searchDay1 = "";
  var searchDay2 = "";
  var nTerm = 0;
  var nNextMonth = 0;
  var EndDayarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  fromDate = new Date();
  toDate = new Date();
  year = toDate.getYear();
  month = toDate.getMonth()+1;
  day = toDate.getDate();


  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) EndDayarr[1] = "29";


  //from 초기값
  /*nTerm = ( 7*24*60*60*1000);
  var tempDate = fromDate.getTime() - nTerm;
  fromDate.setTime(tempDate);
  */

  year = fromDate.getYear();
  month = fromDate.getMonth() +1;
  day = fromDate.getDate();


  if (val=='1w'){
   nTerm = ( 7*24*60*60*1000);
  } else if (val=='2w'){
   nTerm = ( 14*24*60*60*1000);
  }else if (val=='1m') {
   nTerm = ( EndDayarr[month-1]*24*60*60*1000);
  }else if (val=='3m'){
   
   for (i=0; i<3 ;i++ )
   {
    if (month+i>12){
     nNextMonth = (month+i)%12 -1;
    } else {
     nNextMonth = month+i-1;
    }
    nTerm += (EndDayarr[nNextMonth]*24*60*60*1000);
   }
   
  }else if (val=='6m') {
   for (i=0; i<6 ;i++ )
   {
    if (month+i>12){
     nNextMonth = (month+i)%12 -1;
    } else {
     nNextMonth = month+i-1;
    }
    nTerm += (EndDayarr[nNextMonth]*24*60*60*1000);
   }
  }else if (val=='1y') {
   for (i=0; i<12 ;i++ )
   {
    if (month+i>12){
     nNextMonth = (month+i)%12 -1;
    } else {
     nNextMonth = month+i-1;
    }
    nTerm += (EndDayarr[nNextMonth]*24*60*60*1000);
   }
  }else if (val=='2y') {

    nTerm = (730*24*60*60*1000);

  }
  if (mode=='bef')
  {
    tempDate = toDate.getTime() - nTerm;
  }
  else if (mode=='aft')
  {
   tempDate = toDate.getTime() + nTerm;
  }


  toDate.setTime(tempDate);

  if (month<10)   month = '0'+month;
  if (day<10) day ='0'+day;

  searchDay1 = year + "-" + month + "-" + day;

  year = toDate.getYear();
  month = toDate.getMonth() +1;
  day = toDate.getDate();

  if (month<10)   month = '0'+month;
  if (day<10) day ='0'+day;
  searchDay2 = year + "-" + month + "-" + day;

  if (mode=='bef')
  {
   form.fromdate.value = searchDay2;
   form.todate.value = searchDay1;
  }
  else if (mode=='aft')
  {
   form.fromdate.value = searchDay1;
   form.todate.value = searchDay2;
  }
 }
 
// goSearchDetail();
}

반응형

관련글 더보기