본문 바로가기
프로그래밍/HTML, CSS

자바스크립트 날짜 시간 input 박스에 넣기

by 젤리씨 2020. 5. 4.
728x90

자바스크립트 날짜 시간 input 박스에 넣는 방법 입니다


아래와 같이 스크립트를 넣어주고 바디부분에 로드시에만 값을 넣게 하면 됩니다


년,월,시간,분,초 전부 들어 있으니 원하는 값만 바꿔서 변경해도 됩니다.



<html>

<head>

<title>시간</title>

  <script type="text/javascript"> 

      (function today(){ 

        var _date = new Date(), 

        _year = _date.getFullYear(),_month = _date.getMonth()+1,

        _day = _date.getDate(),

        _hours = _date.getHours(),

        _minutes = _date.getMinutes(),

        _ampm = _hours < 13 ? "오전" : "오후"; 

          _month = _month < 10 ? "0"+_month : ""; 

          _day = _day < 10 ? "0"+_day : ""; 

          _hours = _hours > 12 ? "0"+(_hours - 12) : "0"+_hours; 

        _time = _year + _month + _day;

      }()); 

</script> 

</head>

<body onLoad="document.all('def_date').value=_time">

<input type="text" name="def_date">

</body>

</html>


728x90

댓글