Javascript 날짜 포멧 설정하기

2024. 11. 23. 23:26Javascript

오라클이나, 자바처럼 formatter같은게 없는 것 같음

HH24MI

function formatDateToHH24MM(date) {
    // Extract hours and minutes
    let hours = date.getHours().toString().padStart(2, '0'); // Ensure 2 digits
    let minutes = date.getMinutes().toString().padStart(2, '0'); // Ensure 2 digits

    // Return formatted string
    return hours + minutes;
}

YYYMMDD

function getYearMonthDay(date) {
    let year = date.getFullYear(); // Get the 4-digit year
    let month = (date.getMonth() + 1).toString().padStart(2, '0'); // Get the month (0-based) and pad to 2 digits
    let day = date.getDate().toString().padStart(2, '0'); // Get the day of the month and pad to 2 digits

    return year + month + day;
}

YYYY-MM-DD HH24:MI

function formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');

    return year + '' + monthr + '-' + day + ' ' + hours + ':' + minutes; // Example: '2024-11-21 13:20'
}

'Javascript' 카테고리의 다른 글

Javascript List에 컬럼 및 row 추가  (0) 2024.11.23
Javascript ==와 ===의 차이점  (0) 2024.11.23
자바스크립트에서 redirect 하기  (0) 2024.07.31
javascropt overloading 구현  (0) 2024.07.27
Object 내용 출력  (0) 2024.07.23