상세 컨텐츠

본문 제목

Math, Date 객체

FrontEnd/JavaScript

by H_Develop 2022. 7. 7. 08:14

본문

Math 객체 : 수학 연산을 위한 다양한 메써드 제공
round() 반올림한 정수값
ceil() 올림 정수
floor() 내림한 정수
pow() 거듭제곱
sqrt() 제곱근 (루트)
sbs() 절대값
sin() cos() tan() 사인 코사인 탄젠트 값
min() max() 최소, 최대 값
log() 로그 값

 

    <script>
        document.write(Math.round(5.3) + " round<br>");
        document.write(Math.ceil(5.3) + " ceil<br>");	// 올림
        document.write(Math.floor(5.7) + " floor<br>");	// 내림
        document.write(Math.pow(2,5) + " pow<br>");	// 2의 5승
        document.write(Math.sqrt(73) + " sqrt<br>");	// 73루트
        document.write(Math.abs(-3.7) + " abs<br>");	// 절대값
        document.write(Math.sin(Math.PI/2) + " sin<br>");	// PI 파이
        document.write(Math.cos(0) + " cos<br>");	// cos
        document.write(Math.tan(0) + " tan<br>");	// tan
        document.write(Math.min(0, 3, -5, 12) + " min<br>");	// 최소값
        document.write(Math.max(0, 3, -5, 12) + " max<br>");	// 최대값
        document.write(Math.log(2) + " log<br>");	// log
        document.write(Math.log(3) + " log<br>");	// log
        document.write(Math.round(Math.sqrt(2)) + " round & sqrt");	 // 루트2를 반올림
    </script>
5 round
6 ceil
5 floor
32 pow
8.54400374531753 sqrt
3.7 abs
1 sin
1 cos
0 tan
-5 min
12 max
0.6931471805599453 log	// 
1.0986122886681096 log
1 round & sqrt

 

Date 객체 : 날짜와 시간을 다루는 다양한 메써드 제공
Date()에서 월은 0부터 시작, 10은 11월

 

getFullYear() 4자리 연도(yyyy)를 구함
getMonth() 월(0~11)
getDate() 일(1~31)
getHours() 시간(0~23)
getMinutes() 분(0~59)
getSeconds() 초(0~59)
getDay() 요일(0~6)

 

    <script>
        var d = new Date();
        document.write(d);
    </script>
Wed Jul 06 2022 12:09:38 GMT+0900 (한국 표준시)



    <script>
        var d1 = new Date(2025, 10, 21, 8 ,33 , 30);
        // 2025년 11월 21일 8시 33분 30초, Date()에서 월은 0부터 시작이므로, 10은 11월
        // 시간은 1970년 1월 1일 0시부터의 경과시간
        document.write(d1 + "<br>");

        var d2 = new Date(2028, 3, 30, 15, 20);
        document.write(d2 + "<br>");
        // 2028년 3월 30일 15시 30분

        var d3 = new Date(2019, 7, 15);
        document.write(d3)
        // 2019년 7월 15일
    </script>

Fri Nov 21 2025 08:33:30 GMT+0900 (한국 표준시)
Sun Apr 30 2028 15:20:00 GMT+0900 (한국 표준시)
Thu Aug 15 2019 00:00:00 GMT+0900 (한국 표준시)

 

    <script>
        function showDate () {
            var t = new Date();
            var year = t.getFullYear();
            var month = t.getMonth();
            month++;
            
            var date = t.getDate();
            var hour = t.getHours();
            var minute = t.getMinutes();
            var second = t.getSeconds();
            
            document.write("<h3>현재 시간</h3>")
            document.write(year + " 년 " + month + " 월 " + date + " 일 <br>")
            document.write(hour + " 시 " + minute + " 분 " + second + " 초<br>")
        }
    </script>
</head>
<body>
    <button onclick="showDate()">시간 보기</button>
</body>
현재 시간
2022 년 7 월 6 일
12 시 28 분 2 초

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

관련글 더보기