$.extend({
//=====================================================================================
// 두 날짜간의 차이
// dateType : w : 주 , d : 일, h : 시, n : 분, s : 초
//=====================================================================================
getDateDiff: function(dateType, toDate, fromDate){
var dateType = dateType.toLowerCase();
var fromDate = fromDate == null ? $.getToday(1) : fromDate;
var splitFromDate = fromDate.split("-");
var splitToDate = toDate.split("-");
var divideNum = { w: 604800000, d: 86400000, h: 3600000, n: 60000, s: 1000 };
var dateGap;
fromDate = Date.UTC(splitFromDate[0], parseInt(splitFromDate[1], 10) + 1, splitFromDate[2]);
toDate = Date.UTC(splitToDate[0], parseInt(splitToDate[1], 10) + 1, splitToDate[2]);
dateGap = Math.floor((toDate - fromDate) / divideNum[dateType]);
return dateGap;
},
//=====================================================================================
// 오늘 날짜
//=====================================================================================
getToday: function(dateType){
var today = new Date();
switch (dateType){
case 1 :
today = today.getFullYear() + "-" + $.addZero(today.getMonth() + 1) + "-" + $.addZero(today.getDate());
break;
case 2 :
today = today.getFullYear() + $.addZero(today.getMonth() + 1) + $.addZero(today.getDate());
break;
}
return today;
},
//=====================================================================================
// 10 이하 숫자 앞에 0 삽입
//=====================================================================================
addZero: function(number){
return parseInt(number, 10) < 10 ? "0" + number : number;
}
});
$.getDateDiff('2011-01-30')
'Javascript > jQuery 사용자 함수' 카테고리의 다른 글
10 이하 숫자 앞에 0 삽입, 삭제 (0) | 2012.07.30 |
---|---|
날짜 유효성 체크 (0) | 2012.07.18 |
오늘 날짜 반환 (0) | 2012.01.05 |
input 입력값 숫자 체크 (0) | 2011.11.22 |
오늘 기준 이전, 이후 날짜 구하기 (0) | 2011.11.03 |