본문 바로가기

개발

[javascript] 날짜 YYYYMMDD 식으로 가져오기

function getDateFormat(date, delimiter){

    var newDate = new Date();

    if( date != null ) newDate = date;

    

    var yy = newDate.getFullYear();

    var mm = newDate.getMonth()+1;

    if( mm < 10 ) mm = "0" + mm;

    

    var dd = newDate.getDate();

    if( dd < 10 ) dd = "0" + dd;

    

    if( delimiter == null ) delimiter = "";

    return yy + delimiter + mm + delimiter + dd;

}


*사용법 

- getDateFormat(new Date()) => 20140820

getDateFormat(new Date(),"-") => 2014-08-20