Javascript 如何从今天的日期获得下三个日期

Javascript 如何从今天的日期获得下三个日期,javascript,date,Javascript,Date,我得从明天算起三天。例如,今天的日期是1-12-2016,所以接下来的三个日期是2-12-2016,3-12-2016,4-12-2016 在这种情况下我可以这样做,但我的问题是如果我的日期是30或31我得到的日期是32,33,34,但我必须得到1,2,3 这是我的密码 this.today = new Date(); this.tommorowDate =this.today.setDate(this.today.getDate() + 1) console.log(this

我得从明天算起三天。例如,今天的日期是
1-12-2016
,所以接下来的三个日期是
2-12-2016,3-12-2016,4-12-2016

在这种情况下我可以这样做,但我的问题是如果我的日期是
30
31
我得到的日期是
32,33,34
,但我必须得到
1,2,3

这是我的密码

 this.today = new Date();
    this.tommorowDate =this.today.setDate(this.today.getDate() + 1)
    console.log(this.tommorowDate)
    this.dd = this.today.getDate();
    this.mm = this.today.getMonth()+1;
    this.yyyy = this.today.getFullYear();
    this.currentDate = this.dd+'/'+this.mm+'/'+this.yyyy;

 for(var i = 1; i <= 3; i++){
       var incrementdate = this.dd+i;
        this.datepickerArray.push(this.yyyy+'-'+this.mm+'-'+incrementdate)

    }
this.today=新日期();
this.tommorowDate=this.today.setDate(this.today.getDate()+1)
console.log(this.tommorowDate)
this.dd=this.today.getDate();
this.mm=this.today.getMonth()+1;
this.yyy=this.today.getFullYear();
this.currentDate=this.dd+'/'+this.mm+'/'+this.yyyy;

对于(var i=1;i这不是最好的方法,但希望它能帮助您

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);


var dayAfterTomm = new Date();
dayAfterTomm.setDate(dayAfterTomm.getDate() + 2);

var afterOverMorrow = new Date();
afterOverMorrow.setDate(afterOverMorrow.getDate() + 3);

document.write('<p>'+tomorrow+'</p>')
document.write('<p>'+dayAfterTomm+'</p>')
document.write('<p>'+afterOverMorrow+'</p>')
var明天=新日期();
明天.setDate(明天.getDate()+1);
var dayAfterCommand=新日期();
dayAfteromm.setDate(dayAfteromm.getDate()+2);
var afterOverMorrow=新日期();
afterOverMorrow.setDate(afterOverMorrow.getDate()+3);
文档。写入(“”+明天+“

”) document.write(“”+dayAfterCommand+“

”) 文档。写入(“”+后天+“

”)
不需要创建新变量,您可以通过循环传递值,如
1,2,3
,以获取下一个日期

您也可以使用它,它是一个很棒的库,可以用JavaScript解析、验证、操作和显示日期。对于当前场景,请在下面使用moment js查找代码片段

startdate=新日期();
var new_date=时刻(起始日期)。加上('days',1);
显示日期(新日期);
新日期=时刻(起始日期)。添加('days',2);
显示日期(新日期);
新日期=时刻(起始日期)。添加('days',3);
显示日期(新日期);
函数显示日期(参数)
{
var day=参数格式(“DD”);
变量月份=参数格式(“MM”);
变量年份=参数格式(“YYYY”);
警报(日+月+年);
}

试试这个


变量日期=新日期();
console.log(“今天日期”+日期);
日期=新日期(新日期().setDate(新日期().getDate()+1));
console.log(“Tommorows日期”+日期);
日期=新日期(新日期().setDate(新日期().getDate()+2));
console.log(“tommorows日期后的第二天”+日期);
日期=新日期(new date().setDate(new date().getDate()-1));
控制台日志(“昨天日期”+日期);
试试这个

this.today=新日期();
this.datepickerArray=[];

对于(var i=1;我看了一下。看到这个可能重复的例子了吗?当问题没有标记为矩时,为什么要使用矩.js?@user2181397你完全正确,但我不认为有任何拇指规则,比如如果问题中没有标记,我们就不能给出与之相关的答案,而矩是非常强大的库,超过了对日期的操纵,所以仅供将来参考
this.today = new Date();
this.datepickerArray = [];

 for(var i = 1; i <= 3; i++){
this.today.setDate(this.today.getDate() + 1);
this.datepickerArray.push(this.today.getFullYear()+'-'+(this.today.getMonth()+1)+'-'+this.today.getDate())

    }

    console.log(this.datepickerArray);