2017-01-30 78 views
1

我必须计算三个日期形式的明天。例如todayes日期是1-12-2016所以未来三日将是2-12-2016 , 3-12-2016 , 4-12-2016如何从今天的日期得到下三个日期

我能够做到这一点在这种情况下,但我的问题是,如果我的日期是3031我是获取日期为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.datepickerArray为[31-1-2017,32-1-2017,33-1-2017。

但我必须像这样[31-1-2017,1-2-2017,2-2-2017]。

我能够得到明天的日期,但使用该如何得到我的数组更新与下个月。

+0

看看[this](http://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript)。 – xzoert

+0

看到这个http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date –

+0

可能重复的[添加日期到JavaScript日期](http:// stackoverflow。 com/questions/563406/add-days-to-javascript-date) –

回答

0

这不是做的最好的方式,但希望它会帮助你

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>') 

而不是创造新的变量,你可以通过一个循环传似1,2,3值获得下一个日期

DEMO

1

您可以使用Moment js以及它是分析一个伟大的图书馆,验证,操作和在JavaScript中显示日期。对于目前的情况,请在下面找到使用时刻JS代码段

startdate = new Date(); 
 
var new_date = moment(startdate).add('days', 1); 
 
DisplayDate(new_date); 
 
new_date = moment(startdate).add('days', 2); 
 
DisplayDate(new_date); 
 
new_date = moment(startdate).add('days', 3); 
 
DisplayDate(new_date); 
 

 
function DisplayDate(param) 
 
{ 
 
var day = param.format('DD'); 
 
var month = param.format('MM'); 
 
var year = param.format('YYYY'); 
 
alert(day + '.' + month + '.' + year); 
 
}
<script src="http://davis-design.de/marktadresse/layout/js/moment.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

为什么在使用moment.js的时候没有标记问题? – brk

+0

@ user2181397你是绝对正确的,但我不认为有任何的拇指规则,如果没有标签的问题,我们不能把有关的答案和时刻是非常强大的图书馆操纵日期,所以这只是为了未来的参考 – Curiousdev

1

试试这个

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <script> 
 
     var date = new Date(); 
 
     console.log("Todays date" + date); 
 
     date = new Date(new Date().setDate(new Date().getDate() + 1)); 
 
     console.log("Tommorows date" + date); 
 
     date = new Date(new Date().setDate(new Date().getDate() + 2)); 
 
     console.log("Day after tommorows date" + date); 
 
     date = new Date(new Date().setDate(new Date().getDate() - 1)); 
 
     console.log("Yesterdays date" + date); 
 
    </script> 
 
</head> 
 

 
<body> 
 
</body> 
 

 
</html>

0

试试这个https://jsfiddle.net/z5a07w7m/

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); 
相关问题