210328 JavaScript 날짜/시간 객체 연습_1

ITisIT210·2021년 3월 29일
0

JavaScript

목록 보기
15/18
post-thumbnail
<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="UTF-8">
            <title>Document</title>
    </head>
    <boby>
        <script>
            // 자바스크립트가 가지고 있는 날짜 시간의 객체 (Date Object)
            var now = new Date(); // new는 생성 연산자

            console.log(now);

            var year = now.getFullYear(); // .getFullYear()
            var month = now.getMonth();
            var _date = now.getDate();
            var day = now.getDay();
            var hour = now.getHours();
            var min = now.getMinutes();
            var sec = now.getSeconds();

            // console.log(year, month, _date, day, hour, min, sec);

            // 2021년 3월 28일 00요일 00시 00분 00초
            // 월 -> +1
            // 24시간 -> 12시간
            // 0분 0초 -> 00분 00초
            var days = ["일", "월", "화", "수", "목", "금", "토"];

            if (hour >= 13 || hour <= 24) {
                hour = hour - 12;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            
            if (min < 10) {
                    min = "0" + min;
            }

            if (sec < 10) {
                    sec = "0" + sec;
            }

            console.log(year + "년 " + (month + 1) + "월 " + _date + "일 " + days[day] + "요일 " + hour + "시 " + min + "분 " + sec + "초");
            // setInterval(function() {
            //     console.log(year + "년 " + (month + 1) + "월 " + _date + "일 " + days[day] + "요일 " + hour + "시 " + min + "분 " + sec + "초");
            // }), 1000;

            // 코드 블럭을 지정한 시간마다 반복시키는 방법
            // setInterval(실행구문, 시간(ms));
            /*
            setInterval(function() {
                // 반복될 실행문
                console.log("안녕!");
            }), 1000;
            */
        </script>
    </boby>
    </html>
profile
Engineering is the closest thing to magic that exists in the world.

0개의 댓글