컴퓨터 Javascript 날짜 더하기
2020.11.26 00:41
두 개가 있는데 하나는 새로운 Date 객체를 생성해서 리턴해주는 것으로 해당 메소드가 호출된 객체의 값은 변하지 않는다. 나머지 하나는 Date 객체 그 자체가 바뀌는 것이다.
/**
* @type : prototype_function
* @access : public
* @desc : 자바스크립트의 내장 객체인 Date 객체에 add 메소드를 추가한다. add 메소드는 구분과 입력값에 따라 날짜/시간을 더하고 뺀 새로운 Date 객체를 return 한다.
* <pre>
* 원래 일자가 2019-01-01 00:00:00이라고 하면
* var originDate = new Date(2019, 0, 1);
* var date = originDate.add("day", 45); ==> 2019-02-15 00:00:00 (45일 후), originDate는 변경없음
* or
* var date = originDate.add("month", -20); ==> 2017-05-01 00:00:00 (20개월 전), originDate는 변경없음
* or
* var date = originDate.add("second", 128); ==> 2019-01-01 00:02:08 (128초 후), originDate는 변경없음
* </pre>
* 위의 예에서 date 변수는 new Date()에서 구분과 입력값에 따라 더해진 값이다.
* <pre>
* # type
* year : 연수를 더하고 뺄 때
* month : 개월수를 더하고 뺄 때
* day : 일수를 더하고 뺄 때
* hour : 시를 더하고 뺄 때
* minute : 분을 더하고 뺄 때
* second : 초를 더하고 뺄 때
* # param
* number 타입이어야 함
* 범위의 제한은 없으므로 월의 경우 연도가 변경될 수 있고, 일의 경우 연도/월 변경될 수 있음. 시, 분, 초 모두 동일
* </pre>
* @return : 새롭게 생성되어 변경된 Date Object.
* @author :
*/
Date.prototype.add = function(type, param) {
if(typeof(param) != "number") {return this;}
var returnDate = new Date(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
switch(type)
{
case "year":
returnDate.setYear(this.getFullYear() + param);
break;
case "month":
returnDate.setMonth(this.getMonth() + param);
break;
case "day":
returnDate.setDate(this.getDate() + param);
break;
case "week":
returnDate.setDate(this.getDate() + (param * 7));
break;
case "hour":
returnDate.setHours(this.getHours() + param);
break;
case "minute":
returnDate.setMinutes(this.getMinutes() + param);
break;
case "second":
returnDate.setSeconds(this.getSeconds() + param);
break;
default:
break;
}
return returnDate;
};
/**
* @type : prototype_function
* @access : public
* @desc : 자바스크립트의 내장 객체인 Date 객체에 addThis 메소드를 추가한다. addThis 메소드는 구분과 입력값에 따라 해당 객체 자신의 날짜/시간을 더하고 뺀다.
* <pre>
* 현재 일시가 2019-01-01 00:00:00이라고 하면
* var originDate = new Date(2019, 0, 1);
* originDate.addThis("day", 45); ==> 2019-02-15 00:00:00 (45일 후)
* originDate.addThis("month", -20); ==> 2017-06-15 00:00:00 (20개월 전) (위에서 addThis 로 date 객체를 변경했기 때문임. 위의 행을 실행하지 않았다면 2017-05-01 00:00:00 임)
* originDate.addThis("second", 128); ==> 2017-06-15 00:02:08 (128초 후) (위에서 addThis 로 date 객체를 변경했기 때문임. 위의 행들을 실행하지 않았다면 2019-01-01 00:02:08 임)
* </pre>
* 위의 예에서 date 변수는 현재 날짜/시각 Date에서 구분과 입력값에 따라 변경될 Date 객체이다.
* <pre>
* # type
* year : 연수를 더하고 뺄 때
* month : 개월수를 더하고 뺄 때
* day : 일수를 더하고 뺄 때
* hour : 시를 더하고 뺄 때
* minute : 분을 더하고 뺄 때
* second : 초를 더하고 뺄 때
* # param
* number 타입이어야 함
* 범위의 제한이 없으므로, 월의 경우 연도가 변경될 수 있고, 일의 경우 연도/월 변경될 수 있음. 시, 분, 초 모두 동일
* </pre>
* @return : 변경된 Date Object.
* @author :
*/
Date.prototype.addThis = function(type, param) {
if(typeof(param) != "number") {return this;}
switch(type)
{
case "year":
this.setYear(this.getFullYear() + param);
break;
case "month":
this.setMonth(this.getMonth() + param);
break;
case "day":
this.setDate(this.getDate() + param);
break;
case "week":
this.setDate(this.getDate() + (param * 7));
break;
case "hour":
this.setHours(this.getHours() + param);
break;
case "minute":
this.setMinutes(this.getMinutes() + param);
break;
case "second":
this.setSeconds(this.getSeconds() + param);
break;
default:
break;
}
return this;
};






