JavaScript

날짜객체

은찡안찡 2022. 9. 23. 15:01
<!DOCTYPE html>
<html lang="ko">
<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>JS02.객체.html</title>
    <script>
        //var 객체 = new 클래스명();
        //객체의 속성값은 객체.속성명;으로 얻는다.
        //객체의 메서드 실행은 객체.메서드명();으로 실행한다.
        //자바스크립트의 객체: 내장 객체, 브라우저 객체(BOM: Browser Object Model), 문서 객체(DOM: Ducument Object Model)
        //내장 객체: 문자(String), 날짜(Date), 배열(Array), 수학(Math) 등이 있다.
        //브라우저 객체 모델: Window, Screen, Location, History, Navigator 객체 등이 있다.
        //문서 객체 모델: ducument.getElementById('아이디명');
        var tv = new Object();
        //클래스의 복제물은 객체: 이뮤터블(원본 "불변"의 법칙)
        tv.color = "white"; //setter
        tv.price = "1,850,000";

        var tvColor = tv.color; //getter
        //tv 객체에 color와 price를 출력하는 info() 메서드를 정의하고 info 메서드를 호출한다.
        tv.info = function(){
            document.write(tv.color);
            document.write(tv.price);
            document.write(this.color);
            document.write(this.price);
        };
        tv.info(); //함수 호출


        //학습목표: 날짜 객체를 이용해서 날짜 계산을 할 수 있다.
        //레퍼런스 변수(참조 변수) = new 클래스();
        var nowDay = new Date(); //날짜(Date) 객체 생성: 현재 날짜 정보를 얻음
        var nyear = nowDay.getFullYear(); //현재 '년도'를 얻음
        var nmonth = nowDay.getMonth()+1; //현재 '월'을 얻음(0부터 시작)
        var nday = nowDay.getDate(); //현재 '일'을 얻음
        //var nweek = nowDay.getDay(); //현재 '요일'을 얻음(1=월요일, 7=일요일)
        document.write('<h1>현재 날짜 정보: '+nyear+'년 '+nmonth+'월 '+nday+'일</h1>');


        //현재 날짜 - 2020년 4월 31일 = 친구를 만난 날짜부터 지금까지의 일수가 나온다. 날짜를 계산할려면 날짜 객체를 생성해서 계산을 해야한다. 날짜 객체의 getTime()로 밀리초 단위로 얻어서 계산한다.
        var startDay = new Date(2020, 4, 31); //특정 날짜 객체 생성
        var syear = startDay.getFullYear(); //2020
        var smonth = startDay.getMonth(); //4
        var sday = startDay.getDate(); //31
        
        document.write('<h1>만난 날짜 정보: '+syear+'년 '+smonth+'월 '+sday+'일</h1>');

        var ingday=nowDay.getTime() - startDay.getTime();
        var result=Math.ceil(ingday/(1000*60*60*24));

        document.write('<h3>교재한 일 수: '+result+'일</h3>');

        //※ 연말까지 날짜 계산
        var toDay = new Date(); //Date 객체를 생성하여 현재 날짜 객체를 얻는다.
        //var lastDay = new Date(2023, 11, 31); //month는 11이 12월 지정한다.
        var lastDay = new Date();
        lastDay.setFullYear(2023);
        lastDay.setMonth(11);
        lastDay.setDate(31);
        //Date 객체를 생성하여 특정 날짜를 지정한 객체를 얻는다.
        var diffDate = lastDay.getTime() - toDay.getTime();
        //남은날 = 특정날짜 - 현재날짜(밀리초 단위로 얻은 후 계산한다.)
        result = Math.ceil(diffDate/(1000*60*60*24));
        //Math.ceil(): 올림처리한다.
        /*
        1초 = 1,000(msc)
        1분(60초) = 1,000 x 60                              //60,000(msc)
        1시간(60분) = 1,000 x 60 x 60                 //3,600,000(msc)
        1일(60분x24) = 1,000 x 60 x 60 x 24     //86,400,000(msc)
        */
        document.write('<h3>D-Day: '+result+'일</h3>');
    </script>
</head>
<body>
    
</body>
</html>

'JavaScript' 카테고리의 다른 글

브라우저객체  (0) 2022.09.28
문자열객체  (0) 2022.09.28
배열객체  (0) 2022.09.23
수학객체  (0) 2022.09.23
자바스크립트 시작  (0) 2022.09.23