2017-09-18 233 views
0

我正在編寫Ionic 3項目,我使用FullCalendar插件來顯示日曆UI。 當我嘗試在日曆上更改一天的背景時,頁面模塊中的代碼出現問題。 我已經使用FullCalendar的dayRender函數。date.getDate()不是函數。 (在'date.getDate()','date.getDate()'是未定義的)FullCalendar in Ionic 3

$('#calendar').fullCalendar({ 

      dayRender: function (date, cell) { 
       var today = new Date('2017-09-11T00:40Z') 
       if (date.getDate() == today.getDate()) { 
        this.cell.css("background-color", "red"); 
       } 
      }, 
}); 

我已經在這個輸出運行時錯誤:

date.getDate() is not a function. (In 'date.getDate()', 'date.getDate()' is undefined) FullCalendar in Ionic 3

,所以我不明白爲什麼,因爲日期是已經在FullCalendar庫中定義的Date對象。

任何解決方案?

ps:對不起,我的英語不好。

+0

「數據」 =「日」,......還有...... *「的getData」 =「GETDATE」 –

+0

對不起,我想說的!日期對象 – Lamoichenzio

+0

但是你的錯誤是**數據**。 ** getData **不是函數 –

回答

1

dayRender回調在https://fullcalendar.io/docs/display/dayRender/文檔說

date is the Moment for the given day.

所以date在你的代碼是一個momentJS對象,而不是一個Date對象。這就是getDate方法不存在的原因。你會更好創造today作爲momentJS對象一樣,那麼你可以直接對它們進行比較:

$('#calendar').fullCalendar({ 
    dayRender: function (date, cell) { 
    var today = moment('2017-09-11T00:00Z'); 
    if (date.isSame(today, "day")) { 
     cell.css("background-color", "red"); 
    } 
    }, 
    //... 
}); 

有關我寫的代碼更詳細的信息,請參閱http://momentjs.com/docs/#/parsing/string/的日期,當下的細節構造函數可以解析,並且http://momentjs.com/docs/#/query/is-same/有關日期比較方法的詳細信息。

這裏你可以看到上面的工作示例:http://jsfiddle.net/sbxpv25p/22/

+1

另外,我們可以補充說,你可以在沒有任何參數的情況下通過調用「moment」來「生成」一個真實的今天的日期。 –

+0

我已經讀過日期是一個momentJs對象作爲說文檔,但在我的IDE爲函數日呈現它說: (屬性)dayRender:(日期:日期,單元格:HTMLTableDataCellElement)。 其實如果我使用你的代碼,date.isSame(今天)不工作,因爲日期不是一個momentJS對象(至少根據IDE)。 – Lamoichenzio

+0

不幸的是你的IDE正在討論它的背後:-)。產品本身的文檔將比您的IDE試圖猜測或推斷的文檔更準確。我也剛剛注意到我寫了一些小錯誤,現在我已經在上面的答案中糾正了。你也可以在這裏看到我的代碼的一個工作示例:http://jsfiddle.net/sbxpv25p/21/ – ADyson