JSP&JavaScript&HTML

[Javascript] 날짜 오늘, 이번주, 이번달

헤르메스의날개 2020. 12. 2. 01:26
728x90
<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();" >

 

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.f orm.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.fo rm.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();

 

728x90