如何使用JavaScript将日期添加到当前日期?JavaScript是否有像.NET的AddDay()那样的内置函数?


当前回答

我昨晚创建了这些扩展:可以传递正值或负值;

例子:

var someDate = new Date();
var expirationDate = someDate.addDays(10);
var previous = someDate.addDays(-5);


Date.prototype.addDays = function (num) {
    var value = this.valueOf();
    value += 86400000 * num;
    return new Date(value);
}

Date.prototype.addSeconds = function (num) {
    var value = this.valueOf();
    value += 1000 * num;
    return new Date(value);
}

Date.prototype.addMinutes = function (num) {
    var value = this.valueOf();
    value += 60000 * num;
    return new Date(value);
}

Date.prototype.addHours = function (num) {
    var value = this.valueOf();
    value += 3600000 * num;
    return new Date(value);
}

Date.prototype.addMonths = function (num) {
    var value = new Date(this.valueOf());

    var mo = this.getMonth();
    var yr = this.getYear();

    mo = (mo + num) % 12;
    if (0 > mo) {
        yr += (this.getMonth() + num - mo - 12) / 12;
        mo += 12;
    }
    else
        yr += ((this.getMonth() + num - mo) / 12);

    value.setMonth(mo);
    value.setYear(yr);
    return value;
}

其他回答

感谢Jason的回答,您的回答符合预期,这里是您的代码和AnthonyWJones的便捷格式的混合:

Date.prototype.addDays = function(days){
    var ms = new Date().getTime() + (86400000 * days);
    var added = new Date(ms);
    return added;
}

编辑:您可以这样做,而不是setTime()(或setHours()):

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

var tomorrow = new Date().addDays(1);

Old:

可以使用setHours()代替setTime():

Date.prototype.addDays= function(d){
    this.setHours(this.getHours() + d * 24);
    return this;
};

var tomorrow = new Date().addDays(1);

查看JSFiddle。。。

不使用第二个变量,您可以用接下来的x天替换7:

let d=new Date(new Date().getTime() + (7 * 24 * 60 * 60 * 1000));

正确答案:

function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

回答不正确:

这个答案有时提供正确的结果,但往往返回错误的年份和月份。这个答案唯一有效的时间是您要添加天数的日期恰好是当前的年份和月份。

// Don't do it this way!
function addDaysWRONG(date, days) {
  var result = new Date();
  result.setDate(date.getDate() + days);
  return result;
}

证明/示例

检查此JsFidle

//正确函数addDays(日期,天){var result=新日期(Date);result.setDate(result.getDate()+天);返回结果;}//坏年份/月函数addDaysWRONG(日期,天){var result=新日期();result.setDate(date.getDate()+天);返回结果;}//DST期间不良函数addDaysDstFail(日期,天){var dayms=(天*24*60*60*1000);返回新日期(Date.getTime()+dayms);}//测试函数formatDate(日期){return(date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear();}$('tbody-td:firstchild').each(函数(){var$in=$(此);var$out=$('<td/>').insertAfter($in).addClass(“answer”);var$outFail=$('<td/>').insertAfter($out);var$outDstFail=$('<td/>').insertAfter($outFail);var date=新日期($in.text());var correctDate=formatDate(addDays(日期,1));var failDate=formatDate(addDaysWRONG(日期,1));var failDstDate=formatDate(addDaysDstFail(日期,1));$out.text(correctDate);$outFail.text(失败日期);$outDstFail.text(failDstDate);$outFail.addClass(correctDate==failDate?“right”:“error”);$outDstFail.addClass(correctDate==failDstDate?“right”:“error”);});正文{字体大小:14px;}表{边界塌陷:塌陷;}表格,td,th{边框:1px实心黑色;}标准差{填充:2px;}.错误{颜色:红色;}.对{颜色:绿色;}.答案{字号:粗体;}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js“></script><表><tbody><tr>夏令时日期</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2013年10月3日</td></tr><tr><td>2013年3月11日</td></tr><tr><td>2014年9月3日</td></tr><tr><td>2014年2月11日</td></tr><tr><td>2015年8月3日</td></tr><tr><td>2015年1月11日</td></tr><tr><th colspan=“4”>2013</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2013年1月1日</td></tr><tr><td>2013年1月2日</td></tr><tr><td>2013年1月3日</td></tr><tr><td>2013年1月4日</td></tr><tr><td>2013年1月5日</td></tr><tr><td>2013年1月6日</td></tr><tr><td>2013年1月7日</td></tr><tr><td>2013年1月8日</td></tr><tr><td>2013年1月9日</td></tr><tr><td>1013年1月10日</td></tr><tr><td>2013年1月11日</td></tr><tr><td>2013年1月12日</td></tr><tr><th colspan=“4”>2014</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2014年1月1日</td></tr><tr><td>2014年1月2日</td></tr><tr><td>2014年1月3日</td></tr><tr><td>2014年1月4日</td></tr><tr><td>2014年1月5日</td></tr><tr><td>2014年1月6日</td></tr><tr><td>2014年1月7日</td></tr><tr><td>2014年1月8日</td></tr><tr><td>2014年1月9日</td></tr><tr><td>1014年1月10日</td></tr><tr><td>2014年1月11日</td></tr><tr><td>2014年1月12日</td></tr><tr><th colspan=“4”>2015</th></tr><tr><th>输入</th><th>+1天</th><th>+1天失败</th><th>+1天DST失败</th></tr><tr><td>2015年1月1日</td></tr><tr><td>2015年1月2日</td></tr><tr><td>2015年1月3日</td></tr><tr><td>2015年1月4日</td></tr><tr><td>2015年1月5日</td></tr><tr><td>2015年1月6日</td></tr><tr><td>2015年1月7日</td></tr><tr><td>2015年1月8日</td></tr><tr><td>2015年1月9日</td></tr><tr><td>2015年1月10日</td></tr><tr><td>2015年1月11日</td></tr><tr><td>2015年1月12日</td></tr></tbody></table>

有一个setDate和一个getDate方法,允许您执行以下操作:

var newDate = aDate.setDate(aDate.getDate() + numberOfDays);

如果您想减去天数并将日期格式化为可读格式,则应考虑创建一个自定义DateHelper对象,其外观如下:

var日期助手={addDays:函数(aDate,numberOfDays){aDate.setDate(aDate.getDate()+天数);//添加天数return aDate;//返回日期},格式:函数格式(日期){返回[(“0”+date.getDate()).slice(-2),//获取日期并用零填充(“0”+(date.getMonth()+1)).slice(-2),//获取月份并用零填充date.getFullYear()//获取全年].ejoin('/');//把碎片粘在一起}}//有了这个助手,您现在只需使用一行可读代码即可:// ---------------------------------------------------------------------// 1. 获取当前日期// 2. 增加20天// 3. 格式化它// 4. 输出它// ---------------------------------------------------------------------document.body.innerHTML=DateHelper.format(DateHelper.addDays(new Date(),20));

(另见本Fiddle)