You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复“endDate.getDay is not a function”jQuery报错

报错修复方案

报错核心原因

endDate.getDay is not a function 报错的本质是endDate变量最终不是Date类型,无法调用Date原型链上的getDay方法,由代码中多个语法、逻辑错误共同导致。

具体问题点

  • 变量类型错误:从DOM节点.val-mastering-basic获取的noOfDaysToAdd是字符串类型,未转换为数字就作为循环判断条件,若值为空、非数字会导致循环不执行,endDate保持初始赋值的空字符串,调用getDay直接报错。
  • 数组语法错误:holidays数组内直接写了注释文本,没有加注释标识,破坏了数组结构,会导致后续日期判断逻辑报错,甚至代码完全无法正常执行。
  • 日期API使用错误:JS的Date对象getDay()返回值范围为0(代表周日)到6(代表周六),不存在7、8的返回值,你写的周末跳过判断逻辑完全无效。
  • 冗余无效代码:定义了未使用的变量numberOfDaysToAdd,someDate.setDate(someDate.getDate())属于无意义操作,不会对日期产生任何修改。

修复后代码

jQuery(function($) {
  $.ajaxSetup({
    cache: false
  });
  if ($('body.page-id-5749').length > 0) {
    var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
    var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"];
    var holidays = [
      [2019, 12, 8], // 8 DICEMBRE 2019
      [2019, 12, 25] // 25 DICEMBRE 2019
    ];
    var endDate = new Date(),
      noOfDaysToAdd = parseInt($(".val-mastering-basic").html()) || 1,
      count = 0;
    var someDate = new Date(new Date().toDateString());
    while (count < noOfDaysToAdd) {
      endDate = new Date(someDate.setDate(someDate.getDate() + 1));
      var isHoliday = holidays.find(holiday => endDate.getTime() == new Date(holiday[0], holiday[1] - 1, holiday[2]).getTime());
      if (isHoliday) {
        console.log('holiday, skipping');
      } 
      // 如需跳过周末,替换下方else分支为:else if (endDate.getDay() != 0 && endDate.getDay() != 6)
      else {
        count++;
      }
    }
    $('#date-mastering-basic').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
  }
});

内容的提问来源于stack exchange,提问作者EnglishSound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:09:00