본문 바로가기
▶ Front-End/Javascript

Date Formater

by 오늘도 코딩 2023. 11. 14.
728x90
반응형

Date 타입을 원하는 Format으로 변경하는 메소드

*자세한 설명 생략

 

 

/**
 * Date Formater
 * 
 * @param {*} date 
 * @returns YYYYMMdd
 */
function getFormatDate(date) {
    const YYYY = String(date.getFullYear())
    const MM = String((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
    const dd = String(date.getDate() >= 10 ? date.getDate() : "0" + date.getDate())
    return YYYY + MM + dd
}
/**
 * Date Formater2
 * 
 * @param {*} date 
 * @returns YYYY-MM-dd
 */
function getFormatDate2(date) {
    const YYYY = String(date.getFullYear())
    const MM = String((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
    const dd = String(date.getDate() >= 10 ? date.getDate() : "0" + date.getDate())
    return YYYY + "-" + MM + "-" + dd
}
/**
 * Date Formater3
 * 
 * @param {*} date 
 * @returns [YYYY-MM-dd hh:mm:ss]
 */
function getFormatDate(date) {
    const YYYY = String(date.getFullYear())
    const MM = String((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
    const dd = String(date.getDate() >= 10 ? date.getDate() : "0" + date.getDate())
    const hours = String(date.getHours() >= 10 ? date.getHours() : "0" + date.getHours())
    const minuts = String(date.getMinutes() >= 10 ? date.getMinutes() : "0" + date.getMinutes())
    const seconds = String(date.getSeconds() >= 10 ? date.getSeconds() : "0" + date.getSeconds())
    return "[" + YYYY + "-" + MM + "-" + dd + " " + hours + ":" + minuts + ":" + seconds + "]"
}

 

 

728x90
728x90

'▶ Front-End > Javascript' 카테고리의 다른 글

Start to Last Date  (0) 2023.11.14
Null Check  (0) 2023.11.14

댓글