I'm using this function in a Google Apps Script:
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()
?
How to fix it?
I'm using this function in a Google Apps Script:
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()
?
How to fix it?
Share Improve this question edited Jan 26, 2014 at 13:14 craftApprentice asked Jan 26, 2014 at 12:29 craftApprenticecraftApprentice 2,77720 gold badges61 silver badges87 bronze badges 6- try checking if this is rely Date objects by using ""if (t1 instanceof Date) console.log('this is date');"" – Vlad Nikitin Commented Jan 26, 2014 at 12:36
- Hi, @VladNikitin, I followed your suggestion, but the logs results are strange, the same value seems to get a false and a true result. – craftApprentice Commented Jan 26, 2014 at 12:49
- Are you familiar with chrome dev tools Sources tab, you can watch variables , follow the stack of calls, set breakpoints there. Use it to debug your code and finding out where is the mistake. Or you can give the hole code you are using and i'll try to determine where is the mistake – Vlad Nikitin Commented Jan 26, 2014 at 12:57
- Thanks, @VladNikitin, I'll check it. I put all my code (another script where I got the same problem. Any help? – craftApprentice Commented Jan 26, 2014 at 13:15
-
1
Try using
new Date(2005, 7, 20)
instead ofDate(2005, 7, 20)
everywhere in the code – Vlad Nikitin Commented Jan 26, 2014 at 13:19
2 Answers
Reset to default 1I don't understand this error because clearly Sun Jan 26 2014 10:23:10 GMT-0200 (BRST) is a date object.
Clearly it isn't a Date
object. Perhaps it's a string, or some Google Apps Script object that gets output a bit like a date but doesn't have a getTime
function. Perhaps it's a Range
object containing a date.
If it were a Date
object, it would have the getTime
method.
How to fix it?
Without knowing how you get d1
and d2
, it's impossible to say. If it's a Range
object, for instance, perhaps you can get the date via getValue
.
The first step is to figure out what the object actually is. Perhaps setting a breakpoint on the first line of dataDiff
and inspecting the arguments.
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:
(new Date()).toString();
per ECMA-262:
All of the arguments are optional; any arguments supplied are accepted but are pletely ignored. A String is created and returned as if by the expression (new Date()).toString()…