[Javascript] Date 현재 날짜 출력 (yyyy-mm-dd)

 

Date 함수 활용

let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();

console.log('now =', now); // Date Sun Mar 12 2023 20:59:40 GMT+0900 (대한민국 표준시)
console.log(year); // 2023
console.log(month); // 3
console.log(day); // 12

예제) 'yyyy-mm-dd' 형태로 출력

let nowDate = year + '-' + month.toString().padStart(2,0) + '-' + day.toString().padStart(2,0);
//2023-03-12  
  • "2023-03-12"이 출력된다.

댓글

Designed by JB FACTORY