JavaScript/JavaScript,jQuery

[Javascript] 현재 시간 포맷에 맞게 출력하기

mingmongs 2025. 3. 19. 09:52

title

자바스크립트에서 Date 타입을 사용할 때, 원하는 포맷에 맞게 활용할 수 있는 방법에 대해 소개해보겠다.

 

1) new Date

: 현재 날짜와 시간을 Date 객체로 반환한다.

img1

 

2) 날짜를 포맷에 맞게 출력

const now = new Date();
console.log(now.getFullYear());
console.log(now.getMonth() + 1); // month: 0~11
console.log(now.getDate());
console.log((now.getMonth() + 1).toString().padStart(2, '0'));
console.log(now.getDate().toString().padStart(2, '0'));

 

결과값

img2

 

 

[reference]
https://hianna.tistory.com/857