
new Date() 함수를 사용하여 현재 날짜와 시간을 가져올 때, 날짜와 시간이 실제 현재 날짜와 시간과 일치하는지 확인하는 방법은 다음과 같습니다.
#hostingforum.kr
javascript
const currentDate = new Date();
const now = new Date();
if (currentDate.toISOString().slice(0, 10) === now.toISOString().slice(0, 10)) {
console.log('현재 날짜가 일치합니다.');
} else {
console.log('현재 날짜가 일치하지 않습니다.');
}
또한, new Date() 함수를 사용하여 날짜와 시간을 가져올 때, 날짜와 시간을 형식화하는 방법은 다음과 같습니다.
#hostingforum.kr
javascript
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
const second = String(date.getSeconds()).padStart(2, '0');
console.log(`${year}-${month}-${day} ${hour}:${minute}:${second}`);
이 방법을 사용하면 날짜와 시간을 원하는 형식으로 형식화할 수 있습니다.
2025-06-17 17:57