[Javascript] DateAdd

JavaScript/JavaScript 2012. 7. 14. 22:22
// 예 add_DATEs("2012-12-31", 3) 이면 2013-01-03을 리턴
function add_DATEs(dateformat, dates) {
	dateformat = clear_DateSTRING(dateformat);
	var int_millisecond = 1;
	var int_second      = 1000 * int_millisecond;
	var int_minute      = 60 * int_second;
	var int_hour        = 60 * int_minute;
	var int_day         = 24 * int_hour;
	
	var YY_form = CInt(Left(dateformat, 4));
	var MM_form = CInt(Mid(dateformat, 5, 2))-1;
	var DD_form = CInt(Right(dateformat, 2));
	
	var date = new Date(YY_form, MM_form, DD_form);
	var date_milliseconds = date.valueOf();
	var add_milliseconds  = dates * int_day;
	var ret_date = new Date(date_milliseconds + add_milliseconds);
	
	var year  = ret_date.getFullYear();
	var month = ret_date.getMonth() + 1;
	if ( month < 10 ) {
		month = "0" + month;
	}
	var day   = ret_date.getDate();
	if ( day < 10 ) {
		day = "0" + day;
	}
	
	return ( "" + year + month + day );
}
posted by 뚱2