본문 바로가기

개발

[javascript] 월초, 월말일 구하기

var sDate = "";

var eDate = "";


var nDate = new Date();

    nDate.setDate(1);

alert( getDateFormat(nDate) );


    nDate.setMonth(nDate.getMonth()+1);

    nDate.setDate(nDate.getDate()-1);


alert( getDateFormat(nDate) );


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;

}


http://jsfiddle.net/z7q7asdf/  에서 확인 가능.