拿起日期,添加日期并放回去

时间:2010-09-30 15:59:39

标签: javascript datetime date

我想从屏幕上选择日期,添加1天并放回屏幕。

<input type='text'value='20101231' id='from_date'>

<script>    

    function date_move(direction){              
        var from_date = document.getElementById('from_date').value;                 
        var YYYY = from_date.substring(0,4);
        var MM = from_date.substring(4,6);
        var DD = from_date.substring(6,8);      
        var jsCurDate = new Date( parseInt(YYYY,10), parseInt(MM,10)-1, parseInt(DD,10) );
        var newDate = addOrSubtracDays(jsCurDate,direction)             
        document.getElementById('from_date').value = newDate;   
    }


    function addOrSubtracDays(jsCurDate,daysOffset){        
        var daylength= 1*24*60*60*1000;
        var newDate = new Date(jsCurDate + daylength);
        alert(newDate)      
    }


date_move(+1);

</script>   

2 个答案:

答案 0 :(得分:1)

您无法使用element.value获取任意HTML元素的内容,.value仅适用于表单元素。请改用element.innerHTML

var from_date = document.getElementById('from_date').innerHTML;

另外,您对substring的使用是错误的。我认为substr更清楚:

var YYYY = from_date.substr(0,4);
var MM = from_date.substr(4,2);
var DD = from_date.substr(6,2);

addOrSubtracDays(不应该是addOrSubtractDays?)你需要毫秒数,所以你应该致电Date.getTime。而且,您实际上想要return新值:

return new Date(jsCurDate.getTime() + daylength * daysOffset);

更改#from_date时,您可能希望日期格式相同。为此,我使用新方法扩展Date的原型(并使用辅助函数扩展String):

String.prototype.padLeft = function (length, character) {
    return new Array(length - this.length + 1).join(character || ' ') + this;
};

Date.prototype.toFormattedString = function () {
    return [String(this.getFullYear()),
            String(this.getMonth()+1).padLeft(2, '0'),
            String(this.getDate()).padLeft(2, '0')].join('');
};

在此之后,我们只需拨打toFormattedString

即可
document.getElementById('from_date').innerHTML = newDate.toFormattedString();

顺便说一句,您不需要将YYYYMMDD显式转换为基数为10的整数;那些是自动转换的(不假设以0开头的字符串是八进制数字):

var jsCurDate = new Date(YYYY, MM - 1, DD);

为了清楚起见,完整的脚本现在为:

String.prototype.padLeft = function (length, character) {
    return new Array(length - this.length + 1).join(character || ' ') + this;
};

Date.prototype.toFormattedString = function () {
    return [String(this.getFullYear()),
            String(this.getMonth()+1).padLeft(2, '0'),
            String(this.getDate()).padLeft(2, '0')].join('');
};

function date_move(direction){
    var from_date = document.getElementById('from_date').innerHTML;
    var YYYY = from_date.substr(0,4);
    var MM = from_date.substr(4,2);
    var DD = from_date.substr(6,2);
    var jsCurDate = new Date(YYYY, MM - 1, DD);
    var newDate = addOrSubtracDays(jsCurDate, direction);
    document.getElementById('from_date').innerHTML = newDate.toFormattedString();
}

function addOrSubtracDays(jsCurDate, daysOffset){
    var daylength= 1*24*60*60*1000;
    return new Date(jsCurDate.getTime() + daylength * daysOffset);
}

date_move(+1);

答案 1 :(得分:0)

根据我的理解,该代码中唯一缺少的是:

function addOrSubtracDays(jsCurDate,daysOffset){        
    var daylength= 1*24*60*60*1000;
    var newDate = new Date(jsCurDate+(daylength*daysOffset));
    document.getElementById('from_date').value = newDate; //<-THIS LINE
}

编辑:抱歉,我读错了......没关系