javascript 参考错误:日期未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15123973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:34:56  来源:igfitidea点击:

ReferenceError: date is not defined

javascriptdate

提问by user2084813

I have some code here where I get a value from a form represented by "adate". Then I split the string the user enters at the hyphen and separate each value into year, month and day as you can see. I use those values to define a date object. My console correctly displays the date, but I keep getting this error also showing up. Am I defining the date incorrectly? I'm not sure what the issue is.

我在这里有一些代码,我从“日期”表示的表单中获取值。然后我拆分用户在连字符处输入的字符串,并将每个值分成年、月和日,如您所见。我使用这些值来定义日期对象。我的控制台正确显示日期,但我也不断收到此错误。我是否错误地定义了日期?我不确定是什么问题。

function getFormData() {
    var task = document.getElementById("task").value;
    if (checkInputText(task, "Please enter a task")) return;

    var who = document.getElementById("who").value;
    if (checkInputText(who, "Please enter a person to do the task")) return;

    var adate = document.getElementById("dueDate").value;
    var reString = new RegExp("[0-9]{4}\-\[0-9]{2}\-\[0-9]{2}");

    if ( adate.match(reString)) {
    processDate(adate) } 

    else {
     alert("you did not enter the date in the correct format")
    };

    var id = (new Date()).getTime();
    var todoItem = new Todo(id, task, who, date);
    todos.push(todoItem);
    addTodoToPage(todoItem);
    saveTodoItem(todoItem);

    hideSearchResults();
}    


function processDate(adate) {
     var splitArray = new Array();
     splitArray = adate.split("-");
     var year = splitArray[0];
     var month = splitArray[1] - 1;
     var day = splitArray[2];
     var date = new Date(year, month, day);
     console.log(date);
    }

回答by bfavaretto

Make your function return the date, because the datevariable in there is not visible to the outside:

让你的函数返回日期,因为date那里的变量对外不可见:

function processDate(adate) {
    var splitArray = new Array();
    splitArray = adate.split("-");
    var year = splitArray[0];
    var month = splitArray[1] - 1;
    var day = splitArray[2];
    return new Date(year, month, day);
}

Then assign to a new variable when you call it:

然后在调用它时分配给一个新变量:

var date = processDate(adate);

The error actually originated in the following line, because you were referencing a non-existing datevariable:

该错误实际上起源于以下行,因为您引用了一个不存在的date变量:

var todoItem = new Todo(id, task, who, date);

回答by RobG

Just a comment.

只是一个评论。

The RegExp constructor is usually only required where the expression is dynamically generated. Where you have a fixed expression, it's simpler to use a literal (as you don't have to quote certain characters). Also, to test the format, a more appropriate method is testrather than match.

RegExp 构造函数通常只在动态生成表达式时才需要。如果您有固定表达式,则使用文字会更简单(因为您不必引用某些字符)。此外,要测试格式,更合适的方法是test而不是match.

If the date format is: yyyy-mm-dd, consider:

如果日期格式为:yyyy-mm-dd,请考虑:

var reString = /^\d{4}-\d\d-\d\d$/; // trim leading and trailing white space?

if (reString.test(adate)) {
  processDate(adate);
}

The date string validation should be in the processDatefunction, which might throw different errors depending on whether the format is incorrect or the date is invalid (e.g. 2013-02-29, which will return a date of 2013-03-01 in your current code).

日期字符串验证应该在processDate函数中,这可能会抛出不同的错误,具体取决于格式是否不正确或日期是否无效(例如 2013-02-29,它将在您当前的代码中返回 2013-03-01 的日期)。