日付選択ボックス前進後退

10830 ワード

直接コードをアップ!!!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo</title>
</head>
<body>
<button onclick="next()">   </button>
<button onclick="nextMonth()">   </button>
</body>
<script>
    //   
    function next() {
        // day number,-1:     ;0:     ;1:     ;
        var day = 1
        var today = new Date('2020-12-31')
        var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
        today.setTime(targetday_milliseconds) //   ,       

        var tYear = today.getFullYear()
        var tMonth = today.getMonth()
        var tDate = today.getDate()
        tMonth = this.doHandleMonth(tMonth + 1)
        tDate = this.doHandleMonth(tDate)
        console.log(tYear + '-' + tMonth + '-' + tDate)
    }
    //    
    function nextMonth() {
        var today = new Date('2020-12')
        // day number,-1:     ;0:     ;1:     ;
        var day = getCountDays(today)
        var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day
        today.setTime(targetday_milliseconds) //   ,       

        var tYear = today.getFullYear()
        var tMonth = today.getMonth()
        tMonth = this.doHandleMonth(tMonth + 1)
        console.log(tYear + '-' + tMonth)
    }
    //      
    function doHandleMonth(month) {
        var m = month
        if (month.toString().length == 1) {
            m = '0' + month
        }
        return m
    }
    //         
    function getCountDays(curDate) {
        //       
        var curMonth = curDate.getMonth()
        //        :   curMonth       1,    1
        curDate.setMonth(curMonth + 1)
        //       0
        curDate.setDate(0)
        //        
        return curDate.getDate()
    }
</script>
</html>