简体   繁体   English

如何修复“ .setFullYear不是函数”

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

I receive an error Uncaught TypeError: thisDay.setFullYear is not a function while attempting to modify the values. 我收到错误未捕获的TypeError:尝试修改值时,thisDay.setFullYear不是函数。

I've attempted debugging in various stages of the code and have confirmed that I am able to access the thisDay variable from within my moveDate function, it just does not allow me to use any date functions on it. 我尝试在代码的各个阶段进行调试,并确认我能够从我的moveDate函数中访问thisDay变量,但它不允许我在其上使用任何日期函数。

My code I have been fighting with is located at https://jsfiddle.net/ramseys1990/g9cp42bj/3/ 我一直在与我战斗的代码位于https://jsfiddle.net/ramseys1990/g9cp42bj/3/

The snippet of the main problem is: 主要问题的摘要是:

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;
}

My putCalendar function is: 我的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);
}

And the top of my code file is currently: 目前,我的代码文件的顶部是:

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

I expect it to pass the modified date to my putCalendar function to recreate the calendar for a different year or month. 我希望它会将修改后的日期传递给我的putCalendar函数,以重新创建不同年份或月份的日历。

The problem is that here 问题是这里

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

you call getFullYear and its return value gets passed to putCalendar , which is not a function . 调用 getFullYear并将其返回值传递到不是函数的 putCalendar

Then year , and .setFullYear will get this value, and when you try to call it, it will fail. 然后year.setFullYear将获得此值,并且当您尝试调用它时,它将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM