경영학도의 IT 일지
[자바스크립트] 날짜 객체 설정하기 본문
<script>
let today = new Date(); // today에 new Date(); 객체화하기(날짜)
let nowMonth = today.getMonth(); //today 변수객체에 get시리즈 메서드 적용하여 각 변수마다 값 저장
let nowDate = today.getDate();
let nowDay = today.getDay();
document.write("<h2>" + "금일 날짜 정보" + "</h2>");
document.write("현재 월 : " + nowMonth + "월","</br>");
document.write("현재 일 : " + nowDate + "일","</br>");
document.write("현재 요일 : " + nowDay + "요일","</br>");
let worldCup = new Date(2002, 4, 31); // worldCup에 new Date(); 객체화하기(날짜)
let wMonth = worldCup.getMonth(); //worldCup 변수객체에 get시리즈 메서드 적용하여 각 변수마다 값 저장
let wDate = worldCup.getDate();
let wDay = worldCup.getDay();
document.write("<h2>" + "월드컵 날짜 정보" + "</h2>");
document.write("월드컵 월 : " + wMonth + "월","</br>");
document.write("월드컵 일 : " + wDate + "일","</br>");
document.write("월드컵 요일 : " + wDay + "요일","</br>");
</script>
* 핵심 : 날짜형식으로 객체화하는 것은 new Date();
* get은 불러올때, set은 수정할때 *
<script>
let today = new Date();
let nowYear = today.getFullYear(); //getFullYear() 메서드는 년도만 부르는거
let theDate = new Date(nowYear, 11, 31);
let diffDate = theDate.getTime() - today.getTime(); // theDate(해당날짜까지)에서 today(오늘부터)까지 몇 밀리초 남았는지 구하는 값
let result = Math.ceil( diffDate / ( 1000 * 60 * 60 * 24 )); // 그래서 ( 1000(1초) * 60(1분) * 60(1시간) * 24(1일) ) 로 나누어 주면 1일 단위이며
document.write("연말 D-DAY: " + result + "일 남았습니다!"); // Math.ceil() 메서드 안에 넣어서 그냥 올려서 말해버림
</script>
* 핵심 : 디데이 구할때는 밀리초로 먼저 환산하고 단위를 하루 단위로 바꾼다. 그리고 올림하면 디데이 구할수 있음.
'JavaScript & JQuery' 카테고리의 다른 글
[자바스크립트] 컴퓨터랑 가위바위보 (0) | 2022.04.01 |
---|---|
[자바스크립트] 참조 변수(tv, car) 객체 생성 (0) | 2022.04.01 |
[자바스크립트] 반복문으로 테이블 만들기 (0) | 2022.04.01 |
Comments