펌 : http://lazy-mong.tistory.com/24
<input type='button' value='오늘' onclick="DateSearch.getToday();">
<input type='button' value='이번주' onclick="DateSearch.getThisWeek();">
<input type='button' value='이번달' onclick="DateSearch.getThisMonth();">
<input type='button' value='전체' onclick="DateSearch.resetDate();">
<script type="text/javascript">
DateSearch = function() {
DateSearch.form = document.getElementById('shop_list');
DateSearch.date = new Date();
//올해
DateSearch.date.curYear = DateSearch.date.getYear();
//이번달
DateSearch.date.curMonth = DateSearch.date.getMonth() ;
//오늘
DateSearch.date.curDate = DateSearch.date.getDate();
//요일
DateSearch.date.curDay = DateSearch.date.getDay();
//오늘 YYYY-mm-dd
DateSearch.getToday = function() {
var fullDate = DateSearch.makeFullDate(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate);
DateSearch.form.date_s.value = fullDate;
DateSearch.form.date_e.value = fullDate;
}
//7일뒤 YYYY-mm-dd
DateSearch.getNextSevenDays = function() {
document.shop_list.searchtype.value = 'enddate';
var sevenDaysLater = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate + 6) ;
var nextSevenYear = sevenDaysLater.getFullYear();
var nextSevenMonth = sevenDaysLater.getMonth();
var nextSevenDate = sevenDaysLater.getDate();
//오늘
DateSearch.form.date_s.value = DateSearch.makeFullDate(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate);
//7일뒤
DateSearch.form.date_e.value = DateSearch.makeFullDate(nextSevenYear, nextSevenMonth, nextSevenDate);
}
//15일뒤 YYYY-mm-dd
DateSearch.getNextFiftheenDays = function() {
document.shop_list.searchtype.value = 'enddate';
var fifteenDaysLater = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate + 14) ;
var nextFifteenYear = fifteenDaysLater.getFullYear();
var nextFifteenMonth = fifteenDaysLater.getMonth();
var nextFifteenDate = fifteenDaysLater.getDate();
//오늘
DateSearch.form.date_s.value = DateSearch.makeFullDate(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate);
//15일뒤
DateSearch.form.date_e.value = DateSearch.makeFullDate(nextFifteenYear, nextFifteenMonth, nextFifteenDate);
}
//이번주 YYYY-mm-dd
DateSearch.getThisWeek = function() {
var startOfWeek = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate - DateSearch.date.curDay);
var startYear = startOfWeek.getFullYear();
var startMonth = startOfWeek.getMonth();
var startDate = startOfWeek.getDate();
var endOfWeek = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.curDate + (6- DateSearch.date.curDay)) ;
var endYear = endOfWeek.getFullYear();
var endMonth = endOfWeek.getMonth();
var endDate = endOfWeek.getDate();
//이번주 월요일
DateSearch.form.date_s.value = DateSearch.makeFullDate(startYear, startMonth, startDate);
//이번주 일요일
DateSearch.form.date_e.value = DateSearch.makeFullDate(endYear, endMonth, endDate);
}
//이번달 YYYY-mm-dd
DateSearch.getThisMonth = function() {
//달의 첫째 날
DateSearch.date.startOfMonth = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, 1).getDate();
//달의 마지막 날
DateSearch.date.endOfMonth = new Date(DateSearch.date.curYear, DateSearch.date.curMonth, 0).getDate();
DateSearch.form.date_s.value = DateSearch.makeFullDate(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.startOfMonth);
DateSearch.form.date_e.value = DateSearch.makeFullDate(DateSearch.date.curYear, DateSearch.date.curMonth, DateSearch.date.endOfMonth);
}
//전체
DateSearch.resetDate = function() {
document.shop_list.searchtype.value = '';
DateSearch.form.date_s.value = '';
DateSearch.form.date_e.value = '';
}
// YYYY-mm-dd형식 변환
DateSearch.makeFullDate = function(requestYear, requestMonth, requestDate) {
requestMonth = requestMonth+1;
if (requestMonth < 10) {
requestMonth = '0' + requestMonth;
}
if (requestDate < 10) {
requestDate = '0' + requestDate;
}
DateSearch.date.fullDate = requestYear + '-' + requestMonth + '-' + requestDate;
return DateSearch.date.fullDate;
}
}
DateSearch();
</script>
'언어 > JAVASCRIPT' 카테고리의 다른 글
[JavaScript] 코드 압축 (0) | 2018.06.08 |
---|---|
[Component] 웹에서 엑셀같은 SpreadSheet 써보기 (0) | 2015.12.02 |
[펌]Memory leak 관리 방법 (0) | 2015.12.01 |
자바스크립트 코드 압축 하기 (0) | 2015.11.18 |