我需要能够添加1,2,5或10天到今天的日期使用jQuery。
当前回答
你可以尝试这个,不需要JQuery: timeSolver.js
例如,今天加上5天:
var newDay = timeSolver.add(new Date(),5,"day");
你也可以按小时、月等添加。 请查看更多信息。
其他回答
如果不需要Date中的时间,那么您可以简单地使用Date对象的方法提取月、年和日,并在Day部分添加“n”天。
var n=5; //number of days to add.
var today=new Date(); //Today's Date
var requiredDate=new Date(today.getFullYear(),today.getMonth(),today.getDate()+n)
参考:Mozilla Javascript GetDate
编辑:参考:Mozilla 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);
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf() + days * 24 * 60 * 60 * 1000 );
return dat;
}
你可以使用JavaScript,不需要jQuery:
var someDate = new Date();
var numberOfDaysToAdd = 6;
var result = someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
console.log(new Date(result))
这是5天:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
你不需要JQuery,你可以用JavaScript来做,希望你得到它。