我需要能够添加1,2,5或10天到今天的日期使用jQuery。
当前回答
你可以使用这个库“Datejs开源JavaScript日期库”。
其他回答
你可以使用JavaScript,不需要jQuery:
var someDate = new Date();
var numberOfDaysToAdd = 6;
var result = someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
console.log(new Date(result))
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf() + days * 24 * 60 * 60 * 1000 );
return dat;
}
函数添加天数(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);
您可以像这样扩展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);
你可以尝试这个,不需要JQuery: timeSolver.js
例如,今天加上5天:
var newDay = timeSolver.add(new Date(),5,"day");
你也可以按小时、月等添加。 请查看更多信息。