Programming/javascript

[javascript] 월초 월말

성일만 2015. 5. 11. 11:12

월초 월말



getMonth("first");

getMonth("last");

function getMonth(type){

var nDate = new Date();

nDate.setDate(1);

if("first" == type){

alert( getDateFormat(nDate) );

}

if("last" == type){

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;

}