[REACT] ๐Ÿ“†date <input> ๐ŸŽ‚์ƒ๋…„์›”์ผ ์ œํ•œ ๊ธฐ๋Šฅ

์งฑํšจยท2023๋…„ 12์›” 29์ผ
1

REACT

๋ชฉ๋ก ๋ณด๊ธฐ
27/29

๐Ÿ“†date <input> ๐ŸŽ‚์ƒ๋…„์›”์ผ ์ œํ•œ ๊ธฐ๋Šฅ

ํšŒ์› ์ •๋ณด์—์„œ ๐ŸŽ‚์ƒ๋…„์›”์ผ์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” <input> ๊ฐ’์„ '๋งŒ14์„ธ ๋ถ€ํ„ฐ ๋งŒ110์„ธ ๊นŒ์ง€' ์ œํ•œํ•˜๋Š” min, max๊ฐ’์„ ์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.


์ปดํฌ๋„ŒํŠธ ์™ธ๋ถ€

๐Ÿšฉ๋งŒ ๋‚˜์ด ๋‚ ์งœ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ•จ์ˆ˜ ๋”ฐ๋กœ ๋ถ„๋ฆฌ

export default function calculateDateYearsAgo(years: number) {
  const currentDate = new Date();
  return new Date(currentDate.setFullYear(currentDate.getFullYear() - years))
    .toISOString()
    .split('T')[0];
}

์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€

๐ŸšฉcalculateDateYearsAgo ํ•จ์ˆ˜ import ํ•ด์˜ค๊ธฐ
๐Ÿšฉ๋ฆฌ์•กํŠธ ์ƒ๋‹จ์— ์ƒ๋…„์›”์ผ min, max ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

 //calculateDateYearsAgo ํ•จ์ˆ˜ import
 import calculateDateYearsAgo from '@/utils/calculateDateYearsAgo';
 // ์ƒ๋…„์›”์ผ min, max ๊ฐ’
 const minDate = calculateDateYearsAgo(110);
 const maxDate = calculateDateYearsAgo(14);

๐Ÿšฉinput์•ˆ์— min={minDate}๊ณผ max={maxDate} ๊ฐ’ ์ „๋‹ฌ

 		<table >
              <tbody>
                <tr >
                  <td >
                    <label htmlFor="inputBirthday">์ƒ๋…„์›”์ผ</label>
                  </td>
                  <td >
                    <input
                      type="date"
                      id="inputBirthday"
                      min={minDate}
                      max={maxDate}
                    />
                  </td>
                </tr>
              </tbody>
            </table>

๐Ÿ’โ€โ™‚๏ธ์ตœ์ข… - ๋‚ ์งœ๊ฐ€ ์ œํ•œ๋จ

profile
โœจ๐ŸŒํ™•์žฅํ•ด ๋‚˜๊ฐ€๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹คโœ๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€