[proDoc]날짜 변경 로직

힐링힐링·2023년 4월 28일
0

TS 날짜 변경 로직

예제화면

중도정산근무지정보입력

순서

  1. 시작일을 넣었을때 시작월 값 변경
  2. 시작월을 변경했을때 말일을 고려한 시작일 값 변경
  3. 시작일이 공백인 상태에서 시작월 입력시, 시작일은 시작월의 1일로 변경

예제

  1. 시작일을 넣었을때 시작월 값 변경

[코드]

  cellEditEnd(events : any){

    for(let event of events){
      
      if(event.dataField =="BegDate"){
        this.ss1.setCellValue(event.rowIndex,"BegYm",event.value.substr(0,6));
      }
      
    }
  }

[예제]

  1. 시작월을 변경했을때 말일을 고려한 시작일 값 변경
    A. 시작월을 2023년 2월로 변경시 2023년 2월 31일은 없기 때문에 2023년 2월 28일을 세팅해줍니다.


[코드]

  1. isDate메소드 내부에서 윤달을 체크하여 true/false로 반영하며, false경우 this.coaonutil.getLastDay() 활용하여 해당월에 말일을 반영합니다.
  cellEditEnd(events : any){
    for(let event of events){

      var getCellValue;
      
      if(event.dataField =="BegYm"){
                
        getCellValue = this.ss1.getCellValue(event.rowIndex,"BegDate");
        
        //날짜 유효성검사
        if(this.isDate(event.value+getCellValue.substr(6,2))){
          //시작월 데이터를 시작일 UPDATE 
          this.ss1.setCellValue(event.rowIndex,"BegDate",event.value+getCellValue.substr(6,2));
        }else{ 
          //유효하지않으면 해당월에 마지막일(ex.20230231 => 20230228)
          this.ss1.setCellValue(event.rowIndex,"BegDate",this.coaonutil.getLastDay(event.value+'01'));
        }
      }
      
    }
  }

  isDate(params: any) {
    var year = params.substr(0,4);
    var month = params.substr(4,2);
    var day = params.substr(6,2);

    //yyyymmdd 체크
    if ((month == '04' || month == '06' || month == '09' || month == '11') && day == '31') {
      return false;
    }
    else if (month == '02') {
      //윤달 체크 알고리즘
      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

      if (day > 29 || (day == 29 && !isleap)) {
        return false;
      }
    }
    return true
  }

[예제]

  1. 시작일이 공백인 상태에서 시작월 입력시, 시작일은 시작월의 1일로 변경

[코드]

  1. getCellValue = (getCellValue !== undefined && getCellValue !== "" ) ? getCellValue : this.coaonutil.getFirstDay(event.value+'01')를 활용하여,
    시작월의 1일로 변경합니다.
  cellEditEnd(events : any){

    for(let event of events){

      var getCellValue;

      if(event.dataField =="BegDate"){
        this.ss1.setCellValue(event.rowIndex,"BegYm",event.value.substr(0,6));
      }
      
      if(event.dataField =="BegYm"){
                
        getCellValue = this.ss1.getCellValue(event.rowIndex,"BegDate");

        //getCellValue가 undefined과 ""아니면 getCellValue, undefined과 ""면 시작일
        getCellValue = (getCellValue !== undefined && getCellValue !== "" ) ? getCellValue : this.coaonutil.getFirstDay(event.value+'01');

        //날짜 유효성검사
        if(this.isDate(event.value+getCellValue.substr(6,2))){
          //시작월 데이터를 시작일 UPDATE 
          this.ss1.setCellValue(event.rowIndex,"BegDate",event.value+getCellValue.substr(6,2));
        }else{ 
          //유효하지않으면 해당월에 마지막일(ex.20230231 => 20230228)
          this.ss1.setCellValue(event.rowIndex,"BegDate",this.coaonutil.getLastDay(event.value+'01'));
        }
      }
    }
  }
profile
제가 원하는것은 답이 아닌 문제를 해결하는 방법입니다.

0개의 댓글