[TIL] 2022-11-01

H Kim·2022년 11월 2일
0

TIL

목록 보기
24/72
post-thumbnail

  // 분기설정 버튼 기능
  const handleQuarterStartDate = (buttonNum) => {
    let quarterStartDate = buttonNum;
    if (
      quarterStartDate.month() === 0 ||
      quarterStartDate.month() === 3 ||
      quarterStartDate.month() === 6 ||
      quarterStartDate.month() === 9
    ) {
      quarterStartDate = quarterStartDate.subtract(3, 'month').set('date', 1);
      return quarterStartDate;
    }
    if (
      quarterStartDate.month() === 1 ||
      quarterStartDate.month() === 4 ||
      quarterStartDate.month() === 7 ||
      quarterStartDate.month() === 10
    ) {
      quarterStartDate = quarterStartDate.subtract(4, 'month').set('date', 1);
      return quarterStartDate;
    }
    if (
      quarterStartDate.month() === 2 ||
      quarterStartDate.month() === 5 ||
      quarterStartDate.month() === 8 ||
      quarterStartDate.month() === 11
    ) {
      quarterStartDate = quarterStartDate.subtract(5, 'month').set('date', 1);
      return quarterStartDate;
    }
  };

  const selectSixBeforeQuarter = () => {
    const quarterSixStartDate = handleQuarterStartDate(moment().subtract(15, 'month'));
    setDate({
      startDate: quarterSixStartDate,
      endDate: moment(quarterSixStartDate)
        .set('month', moment(quarterSixStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

  const selectFiveBeforeQuarter = () => {
    const quarterFiveStartDate = handleQuarterStartDate(moment().subtract(12, 'month'));
    setDate({
      startDate: quarterFiveStartDate,
      endDate: moment(quarterFiveStartDate)
        .set('month', moment(quarterFiveStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

  const selectFourBeforeQuarter = () => {
    const quarterFourStartDate = handleQuarterStartDate(moment().subtract(9, 'month'));
    setDate({
      startDate: quarterFourStartDate,
      endDate: moment(quarterFourStartDate)
        .set('month', moment(quarterFourStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

  const selectThreeBeforeQuarter = () => {
    const quarterThreeStartDate = handleQuarterStartDate(moment().subtract(6, 'month'));
    setDate({
      startDate: quarterThreeStartDate,
      endDate: moment(quarterThreeStartDate)
        .set('month', moment(quarterThreeStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

  const selectTwoBeforeQuarter = () => {
    const quarterTwoStartDate = handleQuarterStartDate(moment().subtract(3, 'month'));
    setDate({
      startDate: quarterTwoStartDate,
      endDate: moment(quarterTwoStartDate)
        .set('month', moment(quarterTwoStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

  const selectOneBeforeQuarter = () => {
    const quarterOneStartDate = handleQuarterStartDate(moment());
    setDate({
      startDate: quarterOneStartDate,
      endDate: moment(quarterOneStartDate)
        .set('month', moment(quarterOneStartDate).month() + 3)
        .subtract(1, 'days')
    });
  };

어제에 이어 분기버튼 만들고 있는데 이런 개도라이 같은^^
반복코드가 나오고 있는 중^^
ㅎ ㅏ... 나도 알아... 반복문을 돌리면 된다는 걸...
근데 반복문을 어떻게 돌려야 하는지? 전혀? 모르겠다?
반복문을 돌려야 되는 사실만 알면 반복문은 돌릴 수 없다는 놀라운 사실~!
알고 계셨나요~!
저도 몰랐답니다~!


startOf() & endOf()


분기 처음부터 끝까지 설정 머리를 굴려서 해놨는데... 메소드가 따로 있다고 한다...^^
열받아...^^
나 자신에게...^^


// 원래 해 놓은 코드
const quarterStartDate = moment().subtract(3, 'month').set('date', 1);
	startDate: quarterStartDate,
	endDate: moment(quarterStartDate)
		.set('month', moment(quarterStartDate).month() + 3)
        .subtract(1, 'days')
 
 // 메소드 활용한 코드
 const quarterStartDate = moment().startOf('quarter');
 	startDate: quarterStartDate,
    endDate: moment(quarterStartDate).endOf('quarter');

0개의 댓글