簡體   English   中英

如何修復“ .setFullYear不是函數”

[英]How to fix '.setFullYear is not a function'

我收到錯誤未捕獲的TypeError:嘗試修改值時,thisDay.setFullYear不是函數。

我嘗試在代碼的各個階段進行調試,並確認我能夠從我的moveDate函數中訪問thisDay變量,但它不允許我在其上使用任何日期函數。

我一直在與我戰斗的代碼位於https://jsfiddle.net/ramseys1990/g9cp42bj/3/

主要問題的摘要是:

function moveDate(selection) {

    switch(selection) {
        case "prevYear":
            thisDay.setFullYear((thisDay.getFullYear() - 1).toInteger);
            putCalendar(thisDay.getMonth(), thisDay.getFullYear() - 1);
            break;
        case "prevMonth":
            thisDay.setMonth(thisDay.getMonth() - 1);
            //putCalendar(thisDay.getMonth() - 1, thisDay.getFullYear());
            break;
        case "nextMonth":
            thisDay.setMonth(thisDay.getMonth() + 1);
            //putCalendar(thisDay.getMonth() + 1, thisDay.getFullYear());
            break;
        case "nextYear":
            thisDay.setFullYear(thisDay.getFullYear() + 1);
            //putCalendar(thisDay.getMonth(), thisDay.getFullYear() + 1);
            break;
        case "today":
            thisDay = new Date();
            //putCalendar(thisDay.getMonth(), thisDay.getFullYear());
            break;
    }
    putCalendar(thisDay.getMonth(), thisDay.getFullYear());
    return;
}

我的putCalendar函數是:

function putCalendar(month, year) {


    // Set the date displayed in the calendar 

    thisDay.setMonth = month;
    thisDay.setFullYear = year;
    // Determine the current month
    //var thisMonth = thisDay.getMonth();
    // Determine the current year
    //var thisYear = thisDay.getFullYear();

    // write the calendar to the element with the id 'calendar'
    document.getElementById("demo").innerHTML = createCalendar(thisDay);
}

目前,我的代碼文件的頂部是:

"use strict";
var thisDay = new Date();
putCalendar(thisDay.getMonth(), thisDay.getFullYear());

我希望它會將修改后的日期傳遞給我的putCalendar函數,以重新創建不同年份或月份的日歷。

問題是這里

putCalendar(thisDay.getMonth(), thisDay.getFullYear());

調用 getFullYear並將其返回值傳遞到不是函數的 putCalendar

然后year.setFullYear將獲得此值,並且當您嘗試調用它時,它將失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM