본문 바로가기

JavaScript

[JavaScript] 다음 달(월) 구하기(일 수가 같아야 하는경우)

반응형


단순히 해당 월로부터 몇 개월(30일)뒤가 아니라. UI 적으로 25일인 경우 다음달의 25일을 딱 나타내야 하는 경우가 생겼었다.

가, 다.



 datepicker.html

<!doctype html>
<html lang="ko">
<head>
<title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
        jQuery(function() {
            jQuery("#valid_date").datepicker({
                dateFormat: 'yy-mm-dd'
            });
        });
        function severalMonthsAfterward(month) {

            var selectDay = new Date(jQuery("#valid_date").val());
            var lastDay = new Date(selectDay.getFullYear(), selectDay.getMonth() + 1, 0).getDate();

            var calcDay = new Date(selectDay.getFullYear(), (selectDay.getMonth() + 1) + month, 0);
            var yyyy = calcDay.getFullYear();
            var mm = calcDay.getMonth() + 1;
            var dd = selectDay.getDate();

            if(lastDay == selectDay.getDate()) {
                dd = new Date(selectDay.getFullYear(), (selectDay.getMonth() + 1) + month, 0).getDate();
            }

            if(mm < 10) { mm = "0" + mm; }
            if(dd < 10) { dd = "0" + dd; }

            jQuery("#valid_date").val(yyyy + "-" + mm + "-" + dd);
        }
    </script>
</head>
<body>
<table>
    <tr>
        <th>날짜</th>
        <td>
            <input type="text" id="valid_date" value="" readonly style="cursor:pointer;color:red;" />
            <input type="button" onClick="severalMonthsAfterward(1)" value="1개월"">
            <input type="button" onClick="severalMonthsAfterward(2)" value="2개월"">
            <input type="button" onClick="severalMonthsAfterward(3)" value="3개월"">
        </td>
    </tr>
</table>
 </body>
</html>

 면01 :

 면02 :


반응형