μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- λμ
- λμ ν μ΄λΈ
- 컨νΈλ‘€λ¬
- λͺ λ Ήμ΄
- JavaScript
- λ³μ
- DATE_FORMAT
- like
- order by
- μΈλΌμΈλ·°
- optionνκ·Έ
- postλ°©μ
- JSP
- Update
- 리λ μ€
- ν¨μ
- JS
- λ°°μ΄
- λ°±ν±
- oracle
- νλ‘κ·Έλλ¨Έμ€
- μ½ν
- Ajax
- μλ°μ€ν¬λ¦½νΈ
- MySQL
- SQL
- ν μ΄λΈ
- λ컀
- Spring
- select
- Today
- Total
bom's happy life
[JS] λ§ λμ΄ κ³μ°νκΈ° λ³Έλ¬Έ
μΈννλ κ°μ΄ λ³κ²½λ λ ν΄λΉ ν¨μλ₯Ό μ€νμμΌ λ§ λμ΄λ₯Ό κ³μ°νλ ν¨μλ§λ€κΈ°
1. HTML μμ κ°μ Έμ€κΈ° :
`nameBirthInput`, `childBirthInput`, `nameAgeInput`, `childAgeInput `λ³μλ€μ κ°κ° HTML μμλ₯Ό κ°μ Έμ€λ getElementById ν¨μλ₯Ό μ¬μ©νμ¬ ν΄λΉ μΈν νλλ€μ μ ννλ€. `name_birth_val`μ μ μ²μ μλ μμΌμ μ λ ₯νλ μΈν νλ, `child_birth_val`μ μλ μλ μμΌμ μ λ ₯νλ μΈν νλ, `name_age_val`μ μ μ²μμ λ§ λμ΄λ₯Ό νμνλ μΈν νλ, `child_age_val`μ μλ μ λ§ λμ΄λ₯Ό νμνλ μΈν νλλ₯Ό κ°λ¦¬ν¨λ€.
2. λ§ λμ΄ κ³μ° ν¨μ(`calculateAge`) :
- calculateAge ν¨μλ μΈν νλμ κ°μ κΈ°λ°μΌλ‘ λ§ λμ΄λ₯Ό κ³μ°νκ³ ν΄λΉ κ°μ μΈν νλμ μ€μ νλ μν μ νλ€.
- currentDate λ³μμλ νμ¬ λ μ§λ₯Ό μ μ₯νλ€.
- inputDate λ³μμλ μΈν νλμ κ°μΈ μλ μμΌμ Date κ°μ²΄λ‘ λ³ννμ¬ μ μ₯νλ€.
- age λ³μμλ νμ¬ μ°λμμ μλ μμΌ μ°λλ₯Ό λΉΌμ μ΄κΈ°κ°μΌλ‘ μ€μ νλ€.
- λ§μ½ νμ¬ μμ΄ μλ μμΌ μλ³΄λ€ μκ±°λ, νμ¬ μμ΄ μλ μμΌ μκ³Ό κ°μ§λ§ νμ¬ μΌμ΄ μλ μμΌ μΌλ³΄λ€ μμ κ²½μ°, λμ΄λ₯Ό 1 λΉΌμ€λ€. μ΄λ μλ μμΌμ΄ μμ§ λλνμ§ μμ κ²½μ°μλ ν μ΄μ λΉΌμΌ νκΈ° λλ¬Έ.
- λ§μ§λ§μΌλ‘ ageInputμ κ³μ°λ λ§ λμ΄λ₯Ό λ¬Έμμ΄λ‘ μ€μ νμ¬ ν΄λΉ μΈν νλμ νμνλ€.
3. μΈννλ κ° λ³κ²½ μ μ΄λ²€νΈ 리μ€λ λ±λ‘ :
`nameBirthInput`κ³Ό `childBirthInput` μΈν νλμ κ°μ΄ λ³κ²½λ λ calculateAge ν¨μλ₯Ό μ€ννλ μ΄λ²€νΈ 리μ€λλ₯Ό λ±λ‘νλ€. μ΄λ₯Ό ν΅ν΄ μλ μμΌμ΄ λ³κ²½λλ©΄ ν΄λΉνλ λ§ λμ΄κ° μλμΌλ‘ κ³μ°λμ΄ νμλ€.
.
.
.
JSP
<table>
<tbody>
<tr>
<th>μ μ²μ μλ
μμΌ</th>
<td>
<input id="name_birth_val" type="date">
λ§ <input id="name_age_val" type="text"> μΈ
</td>
</tr>
<tr>
<th>μλ
μλ
μμΌ</th>
<td>
<input id="child_birth_val" type="date">
λ§ <input id="child_age_val" type="text"> μΈ
</td>
</tr>
</tbody>
</table>
Javascript
<script type="text/javascript">
/* λ§ λμ΄ κ³μ°νκΈ° */
// HTML μμ κ°μ Έμ€κΈ°
const nameBirthInput = document.getElementById('name_birth_val'); //dateμΈν
const childBirthInput = document.getElementById('child_birth_val');
const nameAgeInput = document.getElementById('name_age_val'); //λ§ λμ΄ μΈν
const childAgeInput = document.getElementById('child_age_val');
// λ§ λμ΄ κ³μ° ν¨μ
function calculateAge(input, ageInput) {
const currentDate = new Date();
const inputDate = new Date(input.value);
var age = currentDate.getFullYear() - inputDate.getFullYear();
if (
currentDate.getMonth() < inputDate.getMonth() ||
(currentDate.getMonth() === inputDate.getMonth() &&
currentDate.getDate() < inputDate.getDate())
) {
age--;
}
// λ§ λμ΄ ν
μ€νΈλ‘ λ³ν
ageInput.value = age.toString();
}
// μΈν νλμ κ°μ΄ λ³κ²½λ λ calculateAge ν¨μ μ€ν
nameBirthInput.addEventListener('input', function() {
calculateAge(nameBirthInput, nameAgeInput);
});
childBirthInput.addEventListener('input', function() {
calculateAge(childBirthInput, childAgeInput);
});
</script>