본문 바로가기

   
Programming/Javascript

Javascript 날짜 시간 함수

반응형

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

       <head>

             <title></title>

             <meta name="generator" content="editplus" />

             <meta name="author" content="김종현" />

             <style type="text/css">

                    #txtTime {

                           border:10px black solid;

                           font-size:20pt;

                           font-weight:bold;

                           text-align:center;

                           width:300px;

                    }

             </style>                  

 

             <script type="text/javascript">

                    function ShowTime() {

                           //1. 현재 시간 얻어오기

                           var now = new Date();

                           //alert(now);

                           //alert(now.toLocaleString());

                           //alert(now.getYear());// 2자리로 표현

                           //alert(now.getFullYear());//4자리로 표현

                           //alert(now.getMonth() + 1);// 0~11 출력시에는 + 1을 붙여준다.

                           //alert(now.getDate());

                           //alert(now.getHours());

                           //alert(now.getMinutes());

                           //alert(now.getSeconds());

                           //alert(now.getMilliseconds()); // 1/1000

                           //alert(now.getDay());//요일 0()-6()

 

                           // 11:50:24

 

                           var output = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();

                           //alert(output);

 

                           document.getElementById('txtTime').value = output;

                    }

 

                    function Start() {

                           // 타이머 - 함수

                           //함수 인자값을 2 3개 더줄수 있는 함수가 있다.

                           //setInterval("alert('테스트');", 4000);//주기를 가지고 특정 행동을 취하는 함수 : 첫번쨰 인자 : 시간(얼마주기로?) 1000/1 > 4000 = 4

                           setInterval("ShowTime()", 1000);

 

                    }

 

                    function Test() {

                           //먼저 인풋 박스안에 아이디로 값을 찾는다.

                           //alert(document.getElementById("btn1").value);

                           document.getElementById("btn1").value = "테스트중입니다.";

 

                    }

             </script>

 

       </head>

 

       <!-- 페이지를 읽어들인후 호출된다. onload-->

       <body onload="Start();" onunload="alert('안녕히가세요~');">

             <!-- Ex05.htm -->

             <input type="text" id="txtTime" />

             <hr />

             <input type="button" value=" 현재 시간 " onclick="ShowTime();" />

 

             <input type="button" value=" 자동시간 카운트 " onclick="Start();" />

 

             <input type="button" id="btn1" value="Test" onclick="Test();" />

       </body>

</html>

 

 

반응형