오늘은?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>요일구하기</title>
</head>
<body>
<h1>오늘은?</h1>
<button type="button" onclick="today();">Today</button>
<h3 id="print">
<!-- 오늘날짜와 요일 출력 ( 2023-04-13(목) ) -->
</h3>
<script>
function today(){
let today = new Date();
//연도
let year = today.getFullYear();
//월
let month = today.getMonth()+1;
//일
let date = today.getDate();
//요일
let week = today.getDay();
let finalWeek;
switch(week){
case 0: finalWeek="일"; break;
case 1: finalWeek="월"; break;
case 2: finalWeek="화"; break;
case 3: finalWeek="수"; break;
case 4: finalWeek="목"; break;
case 5: finalWeek="금"; break;
case 6: finalWeek="토"; break;
};
document.getElementById('print').innerText =
`${year}-${month}-${date} (${finalWeek})`;
}
</script>
</body>
</html>
'HTML.CSS.JS > JS' 카테고리의 다른 글
[JS] 형변환 (0) | 2023.04.14 |
---|---|
[JS] 조건문 if, switch / 널병합 연산자 ?? (0) | 2023.04.14 |
[JS] Date 날짜 관련 함수 (0) | 2023.04.13 |
[JS] 주민번호를 넣고 버튼을 누르면, 생년월일 / 성별 / 나이 출력 (0) | 2023.04.13 |
[JS] stringMethod 문자 메서드 (0) | 2023.04.13 |