Javascript 날짜 포멧 설정

2024. 11. 24. 20:07Javascript

const now = new Date();

// Create a custom format: YYYY-MM-DD HH:mm:ss
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0'); // Month is zero-based
const day = now.getDate().toString().padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');

const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(formattedDate); // Example: 2024-11-21 13:45:09