首页 > 解决方案 > 如何修复“.setFullYear 不是函数”

问题描述

我收到一个错误 Uncaught TypeError: thisDay.setFullYear is not a function 尝试修改这些值。

我已经尝试在代码的各个阶段进行调试,并确认我能够从我的 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 函数,以重新创建不同年份或月份的日历。

标签: javascript

解决方案


问题是这里

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

调用 getFullYear它的返回值被传递给putCalendar,这不是一个函数

然后year, and.setFullYear会得到这个值,当你尝试调用它时,它会失败。


推荐阅读