자바스크립트 날짜 계산 함수
<script language="JavaScript">
<!--
/**
param date : Date Objeet
return string "YYYYMMDD"
usage : getDateObjToStr(new Date());
*/
function getDateObjToStr(date){
var str = new Array();var _year = date.getFullYear();
str[str.length] = _year;
var _month = date.getMonth()+1;
if(_month < 10) _month = "0"+_month;
str[str.length] = _month;
var _day = date.getDate();
if(_day < 10) _day = "0"+_day;
str[str.length] = _day
var getDateObjToStr = str.join("");return getDateObjToStr;
}/**
getDateObjToStr 함수 필요
return Today "YYYYMMDD"
*/
function getToday(){
var d = new Date();
var getToday = getDateObjToStr(d);
return getToday;
}/**
데이트 계산 함수
param date : string "yyyymmdd"
param period : int
param period_kind : string "Y","M","D"
param gt_today : boolean
usage : calcDate("20080205",30,"D");
*/
function calcDate(date,period, period_kind,gt_today){var today = getToday();
var in_year = date.substr(0,4);
var in_month = date.substr(4,2);
var in_day = date.substr(6,2);
var nd = new Date(in_year, in_month-1, in_day);
if(period_kind == "D"){
nd.setDate(nd.getDate()+period);
}
if(period_kind == "M"){
nd.setMonth(nd.getMonth()+period);
}
if(period_kind == "Y"){
nd.setFullYear(nd.getFullYear()+period);
}
var new_date = new Date(nd);
var calcDate = getDateObjToStr(new_date);
if(gt_today){ // 금일보다 큰 날짜 반환한다면
if(calcDate > today){
calcDate = today;
}
}
return calcDate;
}
//-->
</script>