日期选择框前进后退

直接上代码!!!

<!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>

你可能感兴趣的:(Java,js,js,java)