js日期点击选择 当前日期的前或者后的每一天

    function change(type) {
        var nowTimestamp = Date.parse(new Date());
        var year = document.getElementById('year').innerHTML;
        var month = document.getElementById('month').innerHTML;
        var day = document.getElementById('day').innerHTML;
        var timestamp = Date.parse(new Date(year + '-' + month + '-' + day));
        if (type == 'prev') {
            var changeTimestamp = timestamp - 24 * 60 * 60 * 1000;
            $('#right').addClass('bagright')
        } else {
            var changeTimestamp = timestamp + 24 * 60 * 60 * 1000;
            if (changeTimestamp > nowTimestamp) {
                $('#right').removeClass('bagright')
                return;
            } else {
                $('#right').addClass('bagright')
            }
        }
        var date = new Date(changeTimestamp);
        var changeYear = date.getFullYear();
        var changeMonth = setDate(date.getMonth() + 1);
        var changeDay = setDate(date.getDate());
        document.getElementById('year').innerHTML = changeYear;
        document.getElementById('month').innerHTML = changeMonth;
        document.getElementById('day').innerHTML = changeDay;
        changeDate = changeYear + '-' + changeMonth + '-' + changeDay;
        timeStr = changeYear + changeMonth + changeDay;
    }
//1-9的时候前边加上0
    function setDate(date) {
        if (date < 10) {
            date = '0' + date;
        }
        return date;
    }

你可能感兴趣的:(JavaScript)