javascript - 如何修复“.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 函数,以重新创建不同年份或月份的日历。
解决方案
问题是这里
putCalendar(thisDay.getMonth(), thisDay.getFullYear());
你调用 getFullYear
它的返回值被传递给putCalendar
,这不是一个函数。
然后year
, and.setFullYear
会得到这个值,当你尝试调用它时,它会失败。
推荐阅读
- html - 如何禁用输入类型=“文件”的文本部分
- javascript - 我想使用带有 chrome 扩展的 querySelector
- laravel - 预定命令更新记录
- python - 将分号分隔的字符串文本文件转换为列并在 Python 中执行排序?
- assembly - 从 NASM 到 MASM
- flutter - Flutter 嵌套小部件 setState 无法按预期工作
- firebase - firebase 在 jenkins 中部署,不知道在哪里使用由 firebase login 创建的令牌:ci
- powerbi - 根据日期切片器更改共享轴/层次结构
- c# - 使用实体框架从 Cosmos Db 获取子文档
- android - 如何强制媒体流调用流?