2017-08-28 146 views
-3

我有一個條件裏面我有一個循環,當我運行的代碼我能夠通過條件,但在我的條件我有一個循環,沒有得到執行。我的循環沒有得到執行

此線之上構造

currentDate = new Date(); 

這條線在我的控制檯類

if (form.value.packType == "Per Week") { 
      console.log("I could able to see this console"); 
      for (var a = this.currentDate.getDate(); a < 8; a++) { 
       console.log("I could not see this console",a) 
      } 
    } 

裏面,我可以看到的「每一週」的價值,但我一個無法看到控制檯在我的日誌裏面。

有人可以幫助我。

+1

「每週」或「每週」? – Vega

+0

我能夠通過條件,只有我無法看到循環內的值 –

+0

通過在if語句之前執行console.log(form.value.packType)來檢查您是否有「每週」 – Vega

回答

0

在您的for循環中,當您這樣做時:a = this.currentDate.getDate();您將得到`28',這會使循環執行條件爲false並且根本不會運行。

您需要的是獲取當前日期並運行7天循環,然後使用setDate()方法獲取日期。這是你應該怎麼做:

if (form.value.packType == "Per Week") { 

    // get the current day 
    let currentDateDay = this.currentDate.getDate(); 

    // get the next 7 dates from currectDate 
    for(var a=1;a<=7;a++){ 
     // Get the next date 
     let nextDate = new Date(); 
     nextDate.setDate(currentDateDay+a); 
     console.log(nextDate); 
    } 
} 

鏈接Plunker Demo

0

由於今天的日期是28(例如)所以a = 28,並且您有像a < 8這樣的條件,所以條件變爲假,它將從循環中退出而不執行單個循環。

0

如果你在你的控制檯中記錄currentDate.getDate(),你將會看到它的值是28.因爲它大於8,所以循環沒有執行。

console.log(currentDate.getDate()) 

也許你應該澄清循環內部的條件是要幫助你。