JavaScript 내장 Date 객체는 Unix Epoch (1970년 1월 1일 00:00:00 UTC) 이후의 시간을 밀리초 단위로 나타냅니다.
Date 객체 인스턴스 생성
const now = new Date(); // 현재 일시
const epoch = new Date(1730000000000); // 밀리초 타임스탬프로부터 생성
const iso = new Date('2026-10-25T14:30:00Z'); // ISO 8601 문자열로부터 생성
const custom = new Date(2026, 9, 25, 14, 30); // 연, 월(0은 1월), 일, 시, 분
날짜 및 시간 조회 메서드 (Getters)
| 메서드 | 로컬 시간 기준 설명 | UTC 기준 대응 메서드 |
|---|---|---|
getFullYear() |
4자리 연도 | getUTCFullYear() |
getMonth() |
월 인덱스 (0부터 11) | getUTCMonth() |
getDate() |
일 (1부터 31) | getUTCDate() |
getDay() |
요일 (0은 일요일) | getUTCDay() |
getHours() |
시 (0부터 23) | getUTCHours() |
getMinutes() |
분 (0부터 59) | getUTCMinutes() |
getSeconds() |
초 (0부터 59) | getUTCSeconds() |
getMilliseconds() |
밀리초 (0부터 999) | getUTCMilliseconds() |
getTime() |
Unix Epoch 기준 밀리초 타임스탬프 | (same timestamp) |
날짜 연산 및 기간 차이 계산
// 날짜에 7일 더하기
const target = new Date();
target.setDate(target.getDate() + 7);
// 두 날짜 간의 일수 차이 계산
const d1 = new Date('2026-10-01');
const d2 = new Date('2026-10-25');
const diffMs = d2 - d1;
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); // => 24
날짜 서식 지정 및 출력
const date = new Date('2026-10-25T14:30:00Z');
date.toISOString(); // "2026-10-25T14:30:00.000Z"
date.toDateString(); // "Sun Oct 25 2026"
date.toTimeString(); // "14:30:00 GMT+0000"
date.toLocaleDateString(); // e.g. "10/25/2026"