📌 JavaScript Date 객체와 요일 이미지 표시
✅ 코드 설명
1. 현재 날짜/시간 가져오기
now = new Date();
- new Date() : 현재 날짜와 시간 정보를 가진 객체 생성
2. Date 출력 메서드
document.write("now1 :" + now + "<br>");
document.write("now2 :" + now.toString() + "<br>");
document.write("now3 :" + now.toDateString() + "<br>");
document.write("now4 :" + now.toTimeString() + "<br>");
- toString() : 전체 날짜·시간 문자열
- toDateString() : 날짜만 출력
- toTimeString() : 시간만 출력
3. 연·월·일·요일 구하기
document.write(now.getYear() + 1900 + "년 "); // 옛날 방식 (1900 더해야 현재연도)
document.write(now.getUTCFullYear() + "년 "); // 최신 방식 (UTC 기준 연도)
document.write(now.getMonth() + 1 + "월 "); // 0부터 시작하므로 +1 필요
document.write(now.getDate() + "일 ");
document.write(now.getDay()); // 요일: 0=일, 1=월, ... 6=토
⚠️ getYear()는 구식이므로 getFullYear() 또는 getUTCFullYear() 사용 권장.
4. 요일별 이미지 배열 만들기
yoil = new Array();
yoil[0] = "../images/sun.gif";
yoil[1] = "../images/mon.gif";
yoil[2] = "../images/tue.gif";
yoil[3] = "../images/wed.gif";
yoil[4] = "../images/thu.gif";
yoil[5] = "../images/fri.gif";
yoil[6] = "../images/sat.gif";
day = now.getDay();
document.write("오늘은 <img src='" + yoil[day] + "'>");
'기초 및 언어 > ▶ JavaScript&JQuery' 카테고리의 다른 글
| 9. JavaScript_팝업창 열기, Window 객체 메서드 (0) | 2025.09.08 |
|---|---|
| 8. JavaScript_Math 메서드/ String 메서드 (0) | 2025.09.08 |
| 6. JavaScript_배열 생성과 특징 정리 (1) | 2025.09.08 |
| 5. JavaScript_반복문(for)과 구구단 (0) | 2025.09.08 |
| 4 JavaScript_prompt로 입력받아 합계/평균을 구하고 학점 판정 (1) | 2025.09.08 |