실무에서 자주 쓰는 날짜(Date) 문법

new Date()

현재 날짜와 시간을 반환합니다.

1
2
const now = new Date();
// Sun Apr 13 2025 20:39:20 GMT+0900 (한국 표준시)

getFullYear(), getMonth(), getDate(), getDay()

날짜, 요일, 년도를 반환합니다.
월의 0은 1월입니다 요일의 0은 일요일입니다.

1
2
3
4
5
const now = new Date();
now.getFullYear(); // 2025
now.getMonth(); // 3
now.getDate(); // 13
now.getDay(); // 0

getHours(), getMinutes(), getSeconds()

시, 분, 초를 반환합니다.

1
2
3
4
const now = new Date();
console.log(now.getHours()); // 20
console.log(now.getMinutes()); // 47
console.log(now.getSeconds()); / 10

setFullYear()

특정 일자, 시간을 설정합니다.
setMonth(), setDate(), setHours(), setMinutes()도 있습니다.

1
2
3
4
const today = new Date();
const monthAgo = new Date(today);
monthAgo.setDate(today.getDate() - 30); // 30일전 날짜 구하기
// Fri Mar 14 2025 20:54:04 GMT+0900 (한국 표준시)

오늘 날짜 포맷팅 하기

1
2
3
4
5
6
7
8
9
const today = new Date();

const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0'); // 월은 0부터 시작하므로 +1
const day = String(today.getDate()).padStart(2, '0');

const formattedDateKorean = `${year}년 ${month}월 ${day}일`;
console.log(formattedDateKorean);
// 2025년 04월 13일