본문 바로가기
기초 및 언어/▶ JavaScript&JQuery

7. JavaScript_Date

by 류딩이 2025. 9. 8.

📌 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] + "'>");