Frontend/React & React.Native &Next.js
React) 날짜 연산( 더하기 뺴기)
도전맨
2021. 12. 31. 09:12
반응형
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>
반응형