2017-01-30 148 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);