카테고리 없음

javascript 한달전 후 날짜구하기

상문이 2024. 5. 14. 11:27
//일정목록
        if( empty(currentDate) ) {
            currentDate = getTodayYMD();
        }
//이전 버튼
        $('button[id=preBtn]').on('click', function(){
            /*한달전 날짜 구하기*/
            currentDate = getPreMonth(currentDate.substr(0,4),currentDate.substr(4,2),currentDate.substr(6,2));
            var calendarEl1 = document.getElementById('yearVal');
            $(calendarEl1).empty()[0].innerHTML = "<span>"+currentDate.substr(0,4)+"&nbsp;&nbsp;년</span><strong>"+currentDate.substr(4,2)+"</strong>월</p>";

            schdMngConstPlanDtl( currentDate, getlastDay(currentDate) );
        });
       
        //다음 버튼
        $('button[id=postBtn]').on('click', function(){
            /*한달후 날짜 구하기*/
            currentDate = getPostMonth(currentDate.substr(0,4),currentDate.substr(4,2),currentDate.substr(6,2));
            var calendarEl1 = document.getElementById('yearVal');
            $(calendarEl1).empty()[0].innerHTML = "<span>"+currentDate.substr(0,4)+"&nbsp;&nbsp;년</span><strong>"+currentDate.substr(4,2)+"</strong>월</p>";

            schdMngConstPlanDtl( currentDate, getlastDay(currentDate) );
        });
       
    /*한달전 날짜 구하기*/
    function getPreMonth(yyyy,mm,dd) {
        var now = new Date(yyyy,(mm-1),dd);
        var preDay = new Date( now.setMonth( now.getMonth() -1 ) );
        return getTodayYMD(preDay.getFullYear(),preDay.getMonth()+1,'01');
    }

    /*한달후 날짜 구하기*/
    function getPostMonth(yyyy,mm,dd) {
        var now = new Date(yyyy,(mm-1),dd);
        var postDay = new Date( now.setMonth( now.getMonth() +1 ) );
        return getTodayYMD(postDay.getFullYear(),postDay.getMonth()+1,'01');
    }
    /* 해당달의 마지막 날짜 구하기 */
    function getlastDay(source) {
        source = source.replace(/\D/ig, "");
        if (isNaN(source) || source.length < 6) {
            return "";
        }
        var year  = source.substring(0, 4);
        var month = source.substring(4, 6);
        var newDate  = new Date(year, month, 0);

        var newYear  = newDate.getFullYear();
        var newMonth = newDate.getMonth()+1;
        var newDay   = newDate.getDate();
        console.log(newMonth);

        if (newMonth.toString().length < 2) newMonth = "0" + newMonth;
        if (newDay.toString().length < 2) newDay = "0" + newDay;

        return newYear.toString() + newMonth.toString() + newDay.toString();
       
    }

 

참조 :  [JavaScript] 한달 전/후 날짜 구하기 (tistory.com)

 

[JavaScript] 한달 전/후 날짜 구하기

Intro JavaScript 에서 현재 시간을 기준으로 한달 전/후 날짜를 구하는 방법에 대해 알아보도록 하겠습니다. How to solve the problem let now = new Date();// 현재 날짜 및 시간 console.log("현재 : ", now); // 현재 :

steady-learner.tistory.com