HTML/CSS/JS

[JS] 현재 시간 출력, if문 오전 오후 구분하기 (date(),getMonth(),getHours(),if,else)

DEV장화 2022. 10. 18. 15:58
728x90
반응형

 

 

 

 

ㄱ. 현재시간 출력하기

// 현재시간 구하기

let alpha = new Date();

let year = alpha.getFullYear();
let month = alpha.getMonth() + 1;
let day = alpha.getDay();
let hours = alpha.getHours();
let minutes = alpha.getMinutes();
let seconds = alpha.getSeconds();

alert(alpha)

 

 

getMonth() 

: 반환값이 0~11이기 때문에 뒤에 + 1을 해야 함. 

 

 

ㄴ. if문으로 오전 오후 출력하

 

    
    // if 문으로 오전 오후 구하기
    
      let date = new Date();
      let hours = date.getHours();


    if(hours<12){
        alert('오전입니다.')
    } else {
        alert('오후입니다.')
    }

 

 

728x90
반응형