study/Front
java script 오늘날짜 입력
intseq
2023. 10. 25. 10:18
<!DOCTYPE html>
<html>
<head>
<title>오늘 날짜 입력</title>
</head>
<body>
<input type="text" id="dateInput">
<script>
// 오늘 날짜 가져오기
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0'); // 월은 0부터 시작하므로 1을 더하고 두 자리로 포맷팅
const day = String(today.getDate()).padStart(2, '0'); // 일자를 두 자리로 포맷팅
// yyyy-mm-dd 형식으로 날짜 문자열 생성
const formattedDate = `${year}-${month}-${day}`;
// input 요소에 날짜 설정
document.getElementById('dateInput').value = formattedDate;
</script>
</body>
</html>
HTML의 input 요소의 type 속성을 "text"로 설정하고 사용자가 오늘 날짜를 입력할 수 있도록 하려면 JavaScript를 사용하여 오늘 날짜를 가져와서 input 요소의 value 속성에 설정해야 합니다.
이 코드에서는 JavaScript를 사용하여 오늘 날짜를 가져오고, 이 날짜를 "yyyy-mm-dd" 형식의 문자열로 변환한 다음 input 요소의 value 속성에 설정합니다. 결과적으로 input 필드에 오늘 날짜가 표시됩니다.