Language/JavaScript

date함수 예제

IT새내기IM 2023. 1. 7. 17:27

<script>
    var now =new Date();
    
    var year = now.getFullYear(); //년
    //set 입력을 하겠다. get 출력을하겠다
    var month= now.getMonth() + 1; //0~11월이라 +! 줘야함. //월
    var day = now.getDate(); //일
    var day2 = now.getDay(); //요일 단위를 숫자로 표기 일요일0~ 토툐일6까지
    
    var day3 = "";
    if(day2 == 0){
        day3 = "일요일";
    }else if(day2 == 1){
        day3 = "월요일";
    }else if(day2 == 2){
        day3 = "화요일";
    }else if(day2 == 3){
        day3 = "수요일";
    }else if(day2 == 4){
        day3 = "목요일";
    }else if(day2 == 5){
        day3 = "금요일";
    }else if(day2 == 6){
        day3 = "토요일";
    }
    
    var hour = now.getHours(); //시
    var minute = now.getMinutes(); //분
    var second = now.getSeconds(); //초
    var millisecond = now.getMilliseconds(); // 1/1000초 0~999
    
    document.write(hour+"시 ");
    document.write(minute+"분 ");
    document.write(second+"초 ");
    document.write(millisecond+"밀리언초 ");
    
    document.write(year+"년"+month+"월"+day+"일 ");
    document.write(day+"일"+day3);
    
    if(second % 2 == 0){
    document.body.style.background = "red";
    }else{
    document.body.style.background = "blue";   
    }
    
        
</script>