javascript 如何解决这个“类型错误:在对象中找不到函数 getTime”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21363333/
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-27 20:44:21  来源:igfitidea点击:

How to fix this "TypeError: Cannot find function getTime in object"?

javascriptgoogle-apps-script

提问by craftApprentice

I'm using this function in a Google Apps Script:

我在 Google Apps 脚本中使用这个函数:

function dateDiffYears() {
  var datas = [ [Date(2005, 7, 20), Date(2004, 2, 17)],
              [Date(2008, 9, 1),Date(2007, 5, 25)], 
              [Date(2010, 11, 2),Date(2010, 7, 28)], 
              [Date(2014, 0, 24),Date(2013, 1, 27)] ];

  for (var i= 0; i < datas.length; i++){
    var d1 = datas[i][0];
    var d2 = datas[i][1];
  Logger.log("d2  = " + d2 );
  Logger.log("d2 instanceof Date = " + (d2 instanceof Date) );
  Logger.log("d1  = " + d1 );
  Logger.log("d1 instanceof Date = " + (d1 instanceof Date) );
    var t2 = d2.getTime();
    var t1 = d1.getTime();
    Logger.log("d1 = " + t1);
    Logger.log("d2 = " + t2);
    var result =  parseInt( (t2-t1)/(24*3600*1000) );
    Logger.log("diff = " + result);
       }
}

As I can remember, this function worked in the past. But now, when I run it, I got this error message: TypeError: Cannot find function getTime in object Sun Jan 26 2014 10:23:10 GMT-0200 (BRST). (line 22,(line 22 is var t2 = d2.getTime();). I don't understand this error. What is wrong with the Date(yyyy, mm, dd) formate? And why the error message is talking about today date (ex:Sun Jan 26 2014 10:23:10 GMT-0200 (BRST)) since I'm not using New Date()?

我记得,这个功能在过去有效。但是现在,当我运行它时,我收到此错误消息:(TypeError: Cannot find function getTime in object Sun Jan 26 2014 10:23:10 GMT-0200 (BRST). (line 22,第 22 行是var t2 = d2.getTime();)。我不明白这个错误。Date(yyyy, mm, dd) 格式有什么问题?为什么错误消息是在谈论今天的日期(例如:),Sun Jan 26 2014 10:23:10 GMT-0200 (BRST)因为我没有使用New Date()

enter image description here

在此处输入图片说明

How to fix it?

如何解决?

回答by T.J. Crowder

I don't understand this error because clearly Sun Jan 26 2014 10:23:10 GMT-0200 (BRST) is a date object.

我不明白这个错误,因为很明显 Sun Jan 26 2014 10:23:10 GMT-0200 (BRST) 是一个日期对象。

Clearly it isn'ta Dateobject. Perhaps it's a string, or some Google Apps Script object that gets output a bit like a date but doesn't have a getTimefunction. Perhaps it's a Rangeobject containing a date.

显然它不是一个Date对象。也许它是一个字符串,或者一些 Google Apps Script 对象,它的输出有点像日期但没有getTime函数。也许它是一个Range包含日期的对象。

If it were a Dateobject, it would have the getTimemethod.

如果它是一个Date对象,它就会有getTime方法。

How to fix it?

如何解决?

Without knowing how you get d1and d2, it's impossible to say. If it's a Rangeobject, for instance, perhaps you can get the date via getValue.

不知道你是怎么得到d1和的d2,就不可能说出来。Range例如,如果它是一个对象,也许您可​​以通过getValue.

The first step is to figure out what the object actually is. Perhaps setting a breakpoint on the first line of dataDiffand inspecting the arguments.

第一步是弄清楚对象实际上是什么。也许在第一行设置断点dataDiff并检查参数。

回答by RobG

In your code:

在您的代码中:

function dateDiffYears() {
  var datas = [ [Date(2005, 7, 20), Date(2004, 2, 17)],
              [Date(2008, 9, 1),Date(2007, 5, 25)], 
              [Date(2010, 11, 2),Date(2010, 7, 28)], 
              [Date(2014, 0, 24),Date(2013, 1, 27)] ];

creates arrays of strings representing the current date and time, it doesn't create Date objects. Further, all arguments are ignored. Date(andything in here)is equivalent to:

创建表示当前日期和时间的字符串数组,它不创建 Date 对象。此外,所有参数都将被忽略。Date(andything in here)相当于:

(new Date()).toString();

per ECMA-262:

根据ECMA-262

All of the arguments are optional; any arguments supplied are accepted but are completely ignored. A String is created and returned as if by the expression (new Date()).toString()…

所有参数都是可选的;接受提供的任何参数,但完全忽略。一个字符串被创建并返回,就像由表达式 (new Date()).toString()...