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


当前回答

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

其他回答

您可以像这样扩展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);

函数添加天数(n){ var t = new Date(); t.setDate(t.getDate() + n); var month = “0”+(t.getMonth()+1); var date=“0”+t.getDate(); 月 = 月.切片(-2); 日期 = 日期.切片(-2); var date = date +“/”+month +“/”+t.getFullYear(); 警报(日期); } 添加天数(5);

为什么不简单地使用

function addDays(theDate, days) {
    return new Date(theDate.getTime() + days*24*60*60*1000);
}

var newDate = addDays(new Date(), 5);

或者-5减去5天

[更新]在你继续之前考虑阅读这篇文章…

Moment.js

从这里安装moment.js。

NPM: $ NPM I——节省时间

Bower: $ Bower install—节省时间

接下来,

var date = moment()
            .add(2,'d') //replace 2 with number of days you want to add
            .toDate(); //convert it to a Javascript Date Object if you like

链接编号:http://momentjs.com/docs/#/manipulating/add/

Moment.js是一个非常棒的管理Date对象的Javascript库

祝你好运。

Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf() + days * 24 * 60 * 60 * 1000 );
    return dat;
}