본문 바로가기

Front end/JS (JavaScript)

자바스크립트(JavaScript) 시계, 현재시간 (clock)

728x90
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<div class="clock"></div>

<script>
  
  displayCurrentTime()

  function displayCurrentTime() {
    const weekDesc = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
    const date = new Date();
    const year = date.getYear() + 1900;
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const week = date.getDay();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    console.log(week)
    
    const currDt = `${year}년 ${month}월 ${day}일 ${weekDesc[week]} ${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}`  : minutes }:${seconds < 10 ? `0${seconds }`  : seconds }`;

    $(".clock").html(currDt)

    setTimeout(function(){
      displayCurrentTime()
    },1000)
  }
</script>
728x90