[javascript] 날짜(일수) 더하기 빼기 (dateadd)

날자형식의 문자열에 날짜를 빼거나 더할수 있다. 

예) date_add("2011-03-30", 1); //날짜에 1일을 더한다 


function date_add(sDate, nDays) {
    var yy = parseInt(sDate.substr(0, 4), 10);
    var mm = parseInt(sDate.substr(5, 2), 10);
    var dd = parseInt(sDate.substr(8), 10);
 
    d = new Date(yy, mm - 1, dd + nDays);
 
    yy = d.getFullYear();
    mm = d.getMonth() + 1; mm = (mm < 10) ? '0' + mm : mm;
    dd = d.getDate(); dd = (dd < 10) ? '0' + dd : dd;
 
    return '' + yy + '-' +  mm  + '-' + dd;
}

 

 

댓글

Designed by JB FACTORY