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


当前回答

我知道,但有时我喜欢这样:

function addDays(days) {
    return new Date(Date.now() + 864e5 * days);
}

其他回答

一些扩展Date的实现https://gist.github.com/netstart/c92e09730f3675ba8fb33be48520a86d

/**
 * just import, like
 *
 * import './../shared/utils/date.prototype.extendions.ts';
 */
declare global {
  interface Date {
    addDays(days: number, useThis?: boolean): Date;

    addSeconds(seconds: number): Date;

    addMinutes(minutes: number): Date;

    addHours(hours: number): Date;

    addMonths(months: number): Date;

    isToday(): boolean;

    clone(): Date;

    isAnotherMonth(date: Date): boolean;

    isWeekend(): boolean;

    isSameDate(date: Date): boolean;

    getStringDate(): string;
  }
}

Date.prototype.addDays = function(days: number): Date {
  if (!days) {
    return this;
  }
  this.setDate(this.getDate() + days);
  return this;
};

Date.prototype.addSeconds = function(seconds: number) {
  let value = this.valueOf();
  value += 1000 * seconds;
  return new Date(value);
};

Date.prototype.addMinutes = function(minutes: number) {
  let value = this.valueOf();
  value += 60000 * minutes;
  return new Date(value);
};

Date.prototype.addHours = function(hours: number) {
  let value = this.valueOf();
  value += 3600000 * hours;
  return new Date(value);
};

Date.prototype.addMonths = function(months: number) {
  const value = new Date(this.valueOf());

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

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

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

Date.prototype.isToday = function(): boolean {
  const today = new Date();
  return this.isSameDate(today);
};

Date.prototype.clone = function(): Date {
  return new Date(+this);
};

Date.prototype.isAnotherMonth = function(date: Date): boolean {
  return date && this.getMonth() !== date.getMonth();
};

Date.prototype.isWeekend = function(): boolean {
  return this.getDay() === 0 || this.getDay() === 6;
};

Date.prototype.isSameDate = function(date: Date): boolean {
  return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};

Date.prototype.getStringDate = function(): string {
  // Month names in Brazilian Portuguese
  const monthNames = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
  // Month names in English
  // let monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  const today = new Date();
  if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay()) {
    return 'Hoje';
    // return "Today";
  } else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() + 1) {
    return 'Amanhã';
    // return "Tomorrow";
  } else if (this.getMonth() === today.getMonth() && this.getDay() === today.getDay() - 1) {
    return 'Ontem';
    // return "Yesterday";
  } else {
    return this.getDay() + ' de ' + this.monthNames[this.getMonth()] + ' de ' + this.getFullYear();
    // return this.monthNames[this.getMonth()] + ' ' + this.getDay() + ', ' +  this.getFullYear();
  }
};


export {};

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

new Date(+yourDate - 30 *86400000)

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

正确答案:

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>

function addDays(n){
    var t = new Date();
    t.setDate(t.getDate() + n); 
    var month = "0"+(t.getMonth()+1);
    var date = "0"+t.getDate();
    month = month.slice(-2);
    date = date.slice(-2);
     var date = date +"/"+month +"/"+t.getFullYear();
    alert(date);
}

addDays(5);

完成此操作的最简单方法是使用日期fns库。

var addDays = require('date-fns/add_days')
addDays(date, amount)

此链接中提供了文档。您也可以使用moment.js完成此操作

希望它有帮助!