javascript 날짜 계산
script 2009. 1. 12. 21:01몇일부터 몇일까지의 날짜를 계산하기 위한 스크립트
-적용예
~
-소스
-사용
-적용예
~
-소스
<script type="text/javascript">
var dateUtil = function() {
this.startObject = null;
this.endObject = null;
this.args = null;
}
dateUtil.prototype.formatLen = function(str) {
return str = (""+str).length<2 ? "0"+str : str;
}
dateUtil.prototype.formatDate = function(dateObject, delimiter) {
delimiter = delimiter || "-";
return dateObject.getFullYear() + delimiter + this.formatLen(dateObject.getMonth() + 1) + delimiter + this.formatLen(dateObject.getDate());
}
dateUtil.prototype.toDay = function(delimiter) {
return this.formatDate(new Date(), "-");
}
dateUtil.prototype.calDate = function() {
var year = this.args.year == null ? 0 : Number(this.args.year);
var month = this.args.month == null ? 0 : Number(this.args.month);
var day = this.args.day == null ? 0 : Number(this.args.day);
var result = new Date();
result.setYear(result.getFullYear() + year);
result.setMonth(result.getMonth() + month);
result.setDate(result.getDate() + day);
return this.formatDate(result, "-");
}
dateUtil.prototype.setDate = function(startObject, endObject, args) {
this.startObject = startObject;
this.endObject = endObject;
this.args = args;
document.getElementById(this.startObject).value = this.calDate()
document.getElementById(this.endObject).value = this.toDay();
}
var dateUtilObj = new dateUtil();
</script>
var dateUtil = function() {
this.startObject = null;
this.endObject = null;
this.args = null;
}
dateUtil.prototype.formatLen = function(str) {
return str = (""+str).length<2 ? "0"+str : str;
}
dateUtil.prototype.formatDate = function(dateObject, delimiter) {
delimiter = delimiter || "-";
return dateObject.getFullYear() + delimiter + this.formatLen(dateObject.getMonth() + 1) + delimiter + this.formatLen(dateObject.getDate());
}
dateUtil.prototype.toDay = function(delimiter) {
return this.formatDate(new Date(), "-");
}
dateUtil.prototype.calDate = function() {
var year = this.args.year == null ? 0 : Number(this.args.year);
var month = this.args.month == null ? 0 : Number(this.args.month);
var day = this.args.day == null ? 0 : Number(this.args.day);
var result = new Date();
result.setYear(result.getFullYear() + year);
result.setMonth(result.getMonth() + month);
result.setDate(result.getDate() + day);
return this.formatDate(result, "-");
}
dateUtil.prototype.setDate = function(startObject, endObject, args) {
this.startObject = startObject;
this.endObject = endObject;
this.args = args;
document.getElementById(this.startObject).value = this.calDate()
document.getElementById(this.endObject).value = this.toDay();
}
var dateUtilObj = new dateUtil();
</script>
-사용
<input type="text" id="startDate" name="startDate" />~
<input type="text" id="endDate" name="endDate" />
<input type="button" value="오늘" onclick="dateUtilObj.setDate('startDate', 'endDate', {})" />
<input type="button" value="7일" onclick="dateUtilObj.setDate('startDate', 'endDate', {day: -7})" />
<input type="button" value="1달" onclick="dateUtilObj.setDate('startDate', 'endDate', {month: -1})" />
<input type="button" value="1년" onclick="dateUtilObj.setDate('startDate', 'endDate', {year: -1})" />
<input type="text" id="endDate" name="endDate" />
<input type="button" value="오늘" onclick="dateUtilObj.setDate('startDate', 'endDate', {})" />
<input type="button" value="7일" onclick="dateUtilObj.setDate('startDate', 'endDate', {day: -7})" />
<input type="button" value="1달" onclick="dateUtilObj.setDate('startDate', 'endDate', {month: -1})" />
<input type="button" value="1년" onclick="dateUtilObj.setDate('startDate', 'endDate', {year: -1})" />
'script' 카테고리의 다른 글
[Link] execCommand compatibility(호환성) (0) | 2009.05.20 |
---|---|
[Link] Cross Site Scripting Cheat Sheet (0) | 2009.04.29 |
[Link] jQuery Ajax Experience Framework Videos (0) | 2009.02.20 |
javascript getYear() 보다는 getFullYear() (1) | 2009.01.15 |
[Link] Ajax를 마스터 하자 ! (0) | 2009.01.14 |
escapeUrl (0) | 2008.12.07 |
IE7 overflow:hidden bug(?) (3) | 2008.11.21 |
xhtml dtd (0) | 2008.10.04 |
offsetTop / offsetLeft (0) | 2008.10.01 |
[Link] 고급 JavaScript 작성하기 (0) | 2008.09.25 |