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


当前回答

减去30天使用(24小时=86400000ms)

new Date(+yourDate - 30 *86400000)

var yourDate=新日期();var d=新日期(+yourDate-30*86400000)控制台日志(d)

其他回答

这类函数有问题,我用parseInt()解决

Date.prototype.addDays = function(dias) {

    var date = new Date(this.valueOf());
    date.setDate(parseInt(date.getDate()) + parseInt(dias));
    return date;
}

Date.prototype.addMonths = function(months) {
    var date = new Date(this.valueOf());
    date.setMonth(parseInt(date.getMonth()) + parseInt(months));
    return date;
}


Date.prototype.addYears = function(years) {
    var date = new Date(this.valueOf());
    date.setFullYear(parseInt(date.getFullYear()) + parseInt(years));
    return date;
}

我想我也会给出答案:就我个人而言,我喜欢尝试避免无谓的变量声明、方法调用和构造函数调用,因为它们的性能都很昂贵。(当然是合理的)我本打算在@AnthonyWJones给出的答案下留下评论,但考虑得更好。

// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
    return this.setTime( 864E5 * days + this.valueOf() ) && this;
};

// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
    return date.setTime( 864E5 * days + date.valueOf() ) && date;
};

// Basic Function declaration...
function addDaysToDate( date, days ) {
    return date.setTime( 864E5 * days + date.valueOf() ) && date;
};

以上内容将尊重DST。这意味着,如果您添加一个跨越夏令时的天数,则显示的时间(小时)将更改以反映这一点。例子:2014年11月2日02:00是夏令时的结束。

var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt );                  // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) );    // Tue Nov 11 2014 09:30:00

如果你想保留夏令时的时间(所以10:30仍然是10:30)。。。

// Prototype usage...
Date.prototype.addDays = Date.prototype.addDays || function( days ) {
    return this.setDate( this.getDate() + days ) && this;
};

// Namespace usage...
namespace.addDaysToDate = function( date, days ) {
    return date.setDate( date.getDate() + days ) && date;
};

// Basic Function declaration...
function addDaysToDate( date, days ) {
    return date.setDate( date.getDate() + days ) && date;
};

所以,现在你。。。

var dt = new Date( 2014, 10, 1, 10, 30, 0 );
console.log( dt );                  // Sat Nov 01 2014 10:30:00
console.log( dt.addDays( 10 ) );    // Tue Nov 11 2014 10:30:00

短:

函数addDays(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setDate(newDate.getDate()+number));}控制台日志({明天:addDays(新日期(),1)});

预付款:

函数addDays(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setDate(Date.getDate()+number));}函数addMonths(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setMonth(newDate.getMonth()+number));}函数addYears(日期,数字){const newDate=新日期(日期);返回新日期(newDate.setFullYear(newDate.getFullYear)+number);}函数getNewDate(dateTime){let date=新日期();let number=parseInt(dateTime.match(/\d+/)[0]);if(dateTime.indexOf('-')!=-1)number=(-number);如果(dateTime.indexOf('day')!=-1)date=addDays(日期,数字);否则如果(dateTime.indexOf('month')!=-1)date=addMonths(日期,数字);否则如果(dateTime.indexOf('year')!=-1)date=addYears(日期,数字);返回日期;}控制台日志({明天:获取新日期(“+1天”),昨天:getNewDate('-1day'),nextMonth:getNewDate(“+1个月”),nextYear:getNewDate(“+1年”),});

使用jperl提供的修复程序

date d = new Date() // current date

date tomorrow = d.setMonth(d.getMonth(),d.getDate()+1) // return a date incremented by 0 months and 1 day

正确答案:

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>