반응형

15일, 30일, 90일, 180일 나눠서 계산

결과화면

  const [startDate, setStartDate] = useState(new Date())
  const [endDate, setsEndDate] = useState(new Date())
  
    // 날짜 연산 함수 
  const _handle15day = () =>{
    const today = new Date();
    const newDay = new Date();
    return setStartDate(newDay.setDate(today.getDate() - 15 ))
  }
  const _handle30day = () =>{
    const today = new Date();
    const newDay = new Date();
    return setStartDate(newDay.setDate(today.getDate() - 30 ))
  }
  const _handle90day = () =>{
    const today = new Date();
    const newDay = new Date();
    return setStartDate(newDay.setDate(today.getDate() - 90 ))
  }
  const _handle180day = () =>{
    const today = new Date();
    const newDay = new Date();
    return setStartDate(newDay.setDate(today.getDate() - 180 ))
  }
  
  <Box>
    <Button onClick={_handle15day}>15일</Button>
    <Button onClick={_handle30day}>1개월</Button>
    <Button onClick={_handle90day}>3개월</Button>
    <Button onClick={_handle180day}>6개월</Button>
    <DatePicker   selected={startDate} onChange={date => setStartDate(date)} />
    <Text> ~ </Text>
    <DatePicker flex-center' selected={endDate} onChange={date => setEndDate(date)} />
  </Box>

참고: https://7942yongdae.tistory.com/40

반응형
복사했습니다!