我需要能够添加1,2,5或10天到今天的日期使用jQuery。
当前回答
这里有一个对我有用的解决办法。
function calduedate(ndays){
var newdt = new Date(); var chrday; var chrmnth;
newdt.setDate(newdt.getDate() + parseInt(ndays));
var newdate = newdt.getFullYear();
if(newdt.getMonth() < 10){
newdate = newdate+'-'+'0'+newdt.getMonth();
}else{
newdate = newdate+'-'+newdt.getMonth();
}
if(newdt.getDate() < 10){
newdate = newdate+'-'+'0'+newdt.getDate();
}else{
newdate = newdate+'-'+newdt.getDate();
}
alert("newdate="+newdate);
}
其他回答
您可以像这样扩展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);
你可以使用这个库“Datejs开源JavaScript日期库”。
这是5天:
var myDate = new Date(new Date().getTime()+(5*24*60*60*1000));
你不需要JQuery,你可以用JavaScript来做,希望你得到它。
函数添加天数(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);
如果不需要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日期