본문 바로가기

Javascript

Javascript: new Date # 날짜

728x90
반응형

1) 원하는 형태로 조정

const now = new Date(); // create a new Date object with the current date and time

const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0'); // add 1 to the month (since getMonth() returns a zero-based index) and pad with leading zero if necessary
const day = now.getDate().toString().padStart(2, '0'); // pad with leading zero if necessary
const hour = now.getHours().toString().padStart(2, '0'); // pad with leading zero if necessary
const minute = now.getMinutes().toString().padStart(2, '0'); // pad with leading zero if necessary
const second = now.getSeconds().toString().padStart(2, '0'); // pad with leading zero if necessary

const formattedDate = `${year}/${month}/${day} ${hour}:${minute}:${second}`;
console.log(formattedDate); // Output: "2023/04/07 11:20:35"

* 하루 더한 날짜 구하기

let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() +1);

 

 

2) toLocaleString 이용

let tomorrow = new Date();
new_tomorrow = tomorrow.toLocaleString(
    'ko-KR',
    {
        timeZone: 'Asia/Seoul',
        year: 'numeric', month: '2-digit', day: '2-digit',
        hour: '2-digit', minute: '2-digit', second: '2-digit',
        hour12: false
    }
);

 

728x90
반응형