我需要能够添加1,2,5或10天到今天的日期使用jQuery。


当前回答

您可以像这样扩展javascript Date对象

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

在javascript代码中,你可以调用

var currentDate = new Date();
// to add 4 days to current date
currentDate.addDays(4);

其他回答

你可以使用JavaScript,不需要jQuery:

var someDate = new Date();
var numberOfDaysToAdd = 6;
var result = someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
console.log(new Date(result))

您可以像这样扩展javascript Date对象

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(days));
    return this;
};

在javascript代码中,你可以调用

var currentDate = new Date();
// to add 4 days to current date
currentDate.addDays(4);
Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf() + days * 24 * 60 * 60 * 1000 );
    return dat;
}

你可以使用这个库“Datejs开源JavaScript日期库”。

这里公认的答案给了我不可预测的结果,有时会奇怪地加上月和年。

我能找到的最可靠的方法就是在这里 将日期添加到Javascript Date对象,并增加月份

var dayOffset = 20;
var millisecondOffset = dayOffset * 24 * 60 * 60 * 1000;
december.setTime(december.getTime() + millisecondOffset); 

编辑:尽管它对一些人有效,但我不认为它是完全正确的。我建议你用一个更流行的答案,或者使用http://momentjs.com/这样的东西